Python reference issues

Source: Internet
Author: User

1. Absolute reference, relative reference (Absolute_import and relative import)
package/__init__.py
pachage/lala.py
pachage/sys.py

Suppose the import sys in lala.py, is the sys of the current directory referenced, or the SYS in the standard library? Take a look.

lala.py

Import SYS print str (SYS)

sys.py, __init__.py are empty files.

Take a look at the results

Bogon:package licong$ python-m package.lala'package.sys'from'  package/sys.pyc'>

As you can see, Python is a module that follows Sys.path's priority and stops loading if found, and Sys.path is the current directory, so this is the so-called implicit relative reference.

Fortunately, in python2.5 we provide a way to modify this behavior, and take a look at

lala.py revision changed to

 from __future__ Import Absolute_import Import SYS print str (SYS)

Look at the results.

Bogon:package licong$ python-m package.lala'sys' (built-in) >

The difference is just adding the from __future__ import absolute_import this sentence

What if we want to use the SYS module of the current directory? Can be resolved by a relative reference to the display.

lala.py revision changed to

 from __future__ Import Absolute_import Import SYS  from Import sys as haha Print str (SYS) print str (haha)

Bogon:package licong$ python-m package.lala'sys' (built-in' Package.sys '  from ' Package/sys.pyc '>



Summarize some of the reference methods in the package, or examples of file structures in the example

Import Package.sys Absolute References
Import Package.sys Absolute references and binding aliases
An absolute reference that can be substituted from the package import sys
Import SYS implicit relative reference
From. Relative reference for import sys display

2. Circular Import Issues

c1.py

 from Import g def x ():     Pass

c2.py

 from Import x def g ():     Pass


The above two files execute any one of the files will be error, for reasons of c1.py example

1. Execution of C1,
2.c1 the first line needs to refer to the G in C2, execute the C2
3.c2 the first row refers to X in C1 and executes C1
4. Because C1 does not perform, X does not exist
5. Error

Give me a better example.

a.py:

Print " a in " Import SYS Print " B Imported:%s " % ("b"  in Sys.modules,)import  b  print"aout"


b.py:

Print " b in " Import a Print " b out "  = 3

Executive a.py

In onb Imported:truea outb outa out

If you add a a.py in the
Print b.x

The error will be

The reason is that X will only exist after B out, and a out will execute print b.x, while a out is before B out.


Workaround:
1 deferred Import

c1.py

 from Import g def x ():     return g () print x ()


c2.py

def g ():     return 123fromimport x



However, the premise of this modification is that the G function does not depend on C1

2. Import to write inside the function

c1.py

def x ():      from Import g     return g () print x ()



c2.py

def g ():      from Import x      return 123



This will load the response module only when the function is executed, but if you have multiple functions that depend on other modules, you will need to write multiple times and be very foolish.

3. Package
The first thing to be clear, if not in the package, is not a relative reference to this situation.
If it is in the package, the above problem is a workaround (because the package is a whole, each component of the package does not make any sense, but not the package each file can be executed independently)


Assuming that the package structure is
package/__init__.py
package/a.py
package/b.py

Solutions 1
Use import without using from import

a.py

Import package.b def x ():     Pass def haha ():     print package.b.g ()

b.py

Import Package.a def g ():     return 123


You can see, it worked.

Import package.a>>> package.a'package.a' from' Package/a.pyc '>>>> package.a.haha ()


Solutions 2
Write from import to __init__.py
__init__.py

 from import A, b

a.py

Import  Package def x ():     Pass def haha ():     print package.b.g ()

b.py

Import  Package def g ():     return 123




The result is the same.

Python reference issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.