473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

import from question

iu2
Hi all

I've got three files:

file a1.py:
========
the_number = None

file a2.py:
========
import a1

def init():
a1.the_number = 100

file a3.py:
========
from a1 import the_number
import a2

a2.init()
print the_number, type(the_number)

Runninr a3.py I get:
None <type 'NoneType'>

Changing a3.py to:
import a1
import a2

a2.init()
print a1.the_number, type(a1.the_number)

gives:
100 <type 'int'>

Why doesn't it work in the first version of a3.py?

Thanks,
iu2
Jan 14 '08 #1
10 1380
On Jan 14, 4:22 pm, iu2 <isra...@elbit.co.ilwrote:
Hi all

I've got three files:

file a1.py:
========
the_number = None

file a2.py:
========
import a1

def init():
a1.the_number = 100

file a3.py:
========
from a1 import the_number
import a2

a2.init()
print the_number, type(the_number)

Runninr a3.py I get:
None <type 'NoneType'>

Changing a3.py to:
import a1
import a2

a2.init()
print a1.the_number, type(a1.the_number)

gives:
100 <type 'int'>

Why doesn't it work in the first version of a3.py?

Thanks,
iu2
Try to guess what the following snippet prints, run it, and see if you
guessed correctly:

s = {'a':None}
x = s['a']
s['a'] = 1
print x

The same mechanism applies to what "from ... import" does.

HTH,
George
Jan 14 '08 #2
iu2 <is*****@elbit.co.ilwrote:
file a3.py:
========
from a1 import the_number
import a2
....
>
Why doesn't it work in the first version of a3.py?
Think of 'import a2' as being the same as:

a2 = __import__('a2')

and 'from a1 import the_number' as roughly the same as:

the_number = __import__('a1').the_number

In other words think of them as assignments and it should all make sense.

Jan 15 '08 #3
Duncan Booth wrote:
iu2 <is*****@elbit.co.ilwrote:
>file a3.py:
========
from a1 import the_number
import a2
...
>Why doesn't it work in the first version of a3.py?
Think of 'import a2' as being the same as:

a2 = __import__('a2')

and 'from a1 import the_number' as roughly the same as:

the_number = __import__('a1').the_number

In other words think of them as assignments and it should all make sense.
This is a little surprising. So "from mod import *" really copies all of the
scalars into new variables in the local namespace. The same is true with
object pointers I suppose, but this is transparent as all the copies
access the same object.

I always ASSumed that the two forms of import were equivalent, but that
one form did away with the need to be explicit about the namespace: mod.thing
Obviously this is far from the case.
--
Posted via a free Usenet account from http://www.teranews.com

Jan 16 '08 #4
Tobiah <to**@tobiah.orgwrites:
This is a little surprising. So "from mod import *" really copies
all of the scalars into new variables in the local namespace.
No. Nothing is copied. All the objects (remembering that in Python,
*everything* is an object) created by the code in module 'mod' are
given names in the current namespace.
I always ASSumed that the two forms of import were equivalent, but
that one form did away with the need to be explicit about the
namespace: mod.thing Obviously this is far from the case.
Yes. In fact the main difference is in what namespace the module's
objects are made available.

--
\ "The way to build large Python applications is to componentize |
`\ and loosely-couple the hell out of everything." -- Aahz |
_o__) |
Ben Finney
Jan 16 '08 #5
Ben Finney wrote:
Tobiah <to**@tobiah.orgwrites:
>This is a little surprising. So "from mod import *" really copies
all of the scalars into new variables in the local namespace.

No. Nothing is copied. All the objects (remembering that in Python,
*everything* is an object) created by the code in module 'mod' are
given names in the current namespace.
Yeah, copied. Just as in:
>>a = 3
b = a
a = 5
b
3
>>>


given b.py:

##########################
thing = 0
##########################

and a.py:

##########################
from b import *
import b

print thing
print b.thing

b.thing = 1
print b.thing
print thing
###########################

0
0
1
0

--
Posted via a free Usenet account from http://www.teranews.com

Jan 16 '08 #6

"Ben Finney" <bi****************@benfinney.id.auwrote in message
news:87************@benfinney.id.au...
| Tobiah <to**@tobiah.orgwrites:
|
| This is a little surprising. So "from mod import *" really copies
| all of the scalars into new variables in the local namespace.

'Scalar' is not a Python term. Neither is 'object pointer' really,
except in respect to the CPython implementation.

| No. Nothing is copied. All the objects (remembering that in Python,
| *everything* is an object) created by the code in module 'mod' are
| given names in the current namespace.

To amplify, 'from mod import *' is, I believe, more or less equivalent to

import mod
for name in mod.__all__
exec "%s = mod.%s" % name,name
del mod

except, of course, that the imported module would not actually be bound to
'mod'
(and need deleting) so that there is no conflict with mod containing the
name 'mod'.


Jan 16 '08 #7
Tobiah <to**@tobiah.orgwrites:
Ben Finney wrote:
Tobiah <to**@tobiah.orgwrites:
This is a little surprising. So "from mod import *" really copies
all of the scalars into new variables in the local namespace.
No. Nothing is copied. All the objects (remembering that in Python,
*everything* is an object) created by the code in module 'mod' are
given names in the current namespace.

Yeah, copied. Just as in:
>a = 3
b = a
a = 5
b
3
>>
Again, those aren't copies. There is only one instance of each value,
referenced by multiple names. This is made clearer by using a mutable
value:
>>a = [1, 2, 3]
b = a
c = b
a = [4, 5, 6]
a, b, c
([4, 5, 6], [1, 2, 3], [1, 2, 3])
>>a.append("spam")
b.append("eggs")
a, b, c
([4, 5, 6, 'spam'], [1, 2, 3, 'eggs'], [1, 2, 3, 'eggs'])

The value referenced by 'b' and 'c' is one instance; they don't have
copies of the value. Assignment binds a reference to a value, it
doesn't make a copy.

A "copy" is what's implemented by the standard library 'copy' module,
hence the name.

--
\ "Whenever you read a good book, it's like the author is right |
`\ there, in the room talking to you, which is why I don't like to |
_o__) read good books." -- Jack Handey |
Ben Finney
Jan 16 '08 #8
Again, those aren't copies. There is only one instance of each value,
referenced by multiple names.

Ok, I get it. I was locally importing a pointer to an integer which is really
the same object as the module name points to, but the assignment changes that.
The confusion for me centered around the fact that a local name can be used
to change values in mutables that are visible from within the module. This
however, does not include assignment to the local name.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 16 '08 #9
On Wed, 16 Jan 2008 15:31:54 -0800, Tobiah wrote:
>Again, those aren't copies. There is only one instance of each value,
referenced by multiple names.


Ok, I get it. I was locally importing a pointer to an integer
Really? What language were you using? Python doesn't have pointers.

Some *implementations* of Python (e.g. CPython) might have pointers *in
the implementation*, but others (e.g. PyPy, Jython) don't.

which is
really the same object as the module name points to, but the assignment
changes that. The confusion for me centered around the fact that a local
name can be used to change values in mutables that are visible from
within the module. This however, does not include assignment to the
local name.

If you haven't already done so, you should read:

http://effbot.org/zone/python-objects.htm
http://effbot.org/zone/call-by-object.htm
and remember that imports are (more or less) equivalent to assignments.
When you do this:
>>import module
it is roughly equivalent to:
>>module = get_a_module_from_file_name('module')
Alternatively:
>>from module import foo
is roughly equivalent to:
>>temp = get_a_module_from_file_name('module')
foo = temp.foo
del temp

(and the magic function "get_a_module_from_file_name" is actually called
__import__ with double underscores.)
--
Steven
Jan 17 '08 #10
>Ok, I get it. I was locally importing a pointer to an integer

Really? What language were you using? Python doesn't have pointers.
What term do you prefer? Reference? Object id holder?

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kevin MacKenzie | last post by:
I'm a complete newbie to using Python. I have a small question about importing modules. Is there any difference between the two following statements, and what (if any) are they? >>> from...
0
by: Stian Søiland | last post by:
all examples performed with: Python 2.3+ (#2, Aug 10 2003, 11:09:33) on linux2 (2, 3, 0, 'final', 1) This is a recursive import:
1
by: Raaijmakers, Vincent \(GE Infrastructure\) | last post by:
Question: my src path looks like this: src\root\sub1 src\root\sub2 My main code is in root, lets say main.py and there is also a lib.py. In sub1 there if foo1.py and sub2 foo2.py Sorry for...
3
by: Doug Baroter | last post by:
Hi, One of my clients has the following situation. They use Access DB for data update etc. some business functions while they also want to view the Access data quickly and more efficiently in...
2
by: jet | last post by:
Hi, Maybe this is an easy task, but I'm having a really hard time figuring out how to do this. I'm a complete newbie to SQL Server. I have a database dump file from MySQL that's in .sql...
2
by: Charles Fineman | last post by:
I've been asked to look over an integration toolkit that has a bunch of schemas to specify message format. There are a couple of strange things I noticed right off the bat and I wanted to get...
1
by: Dan | last post by:
Could someone please help me with auto importing a series of data files into an Access table. I tried to follow code given below in a previous messagebut i'm getting error messages. Here's my...
4
by: Bruce W. Roeser | last post by:
All, I'm reading a book by Charles Petzold (Programming VS.Net). Pretty good content but am confused about the difference. From the text: ...
3
by: SMALLp | last post by:
Hy! I'm new in Linux, and i feel little less newer in python. I need advice and help. I'm making an application witch purpose is irrelevant. It has a lot of code for now and I've only made...
0
by: 123bargains | last post by:
Hello, I have a question on importing a database from MSSQL 2000 to MSSQL 2005. I hope someone on here can help me answer it. I am trying to import a database from MSSQL 2000 to 2005. But, when I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.