473,386 Members | 1,821 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.

when is a != foo.a?

I am somewhat new to Python (last year). As such I encounter little
"gotchas" all the time. I am wondering is someone can explain this to me:

If have three simple files:

a.py ---------------------

foo = None
def a(b):
global foo
foo = b

b.py ----------------------

from a import foo
def b(): print foo

c.py ----------------------

import a
from b import b

print 'per a.a() ',a.foo
a.a(245)
print 'expect 245 ', a.foo
b()
If I run 'python c.py' I get the following printed out:
per a.a() None
expect 245 245
None
That surprised me. If I change b.py to

import a
def b(): print a.foo

I get the following (which is what I expected originally):
per a.a() None
expect 245 245
245
Can someone explain what is really going on here?

TIA,
Chaz
Aug 28 '06 #1
4 1318
Chaz Ginger wrote:
Can someone explain what is really going on here?
Think of 'from x import y' as an assignment. Roughly equivalent to:

y = sys.modules['x'].y

(except of course you don't have to have imported sys, and it will load the
module 'x' if it hasn't already been imported.)
b.py ----------------------

from a import foo
In other words:

foo = a.foo

foo in module b is initialised from a.foo, but it is a separate variable.
So when a.foo is rebound that doesn't affect b.foo.
def b(): print foo

c.py ----------------------

import a
from b import b
and here:
b = b.b
>
print 'per a.a() ',a.foo
a.a(245)
print 'expect 245 ', a.foo
b()

Aug 28 '06 #2
Duncan Booth wrote:
Chaz Ginger wrote:
>Can someone explain what is really going on here?

Think of 'from x import y' as an assignment. Roughly equivalent to:

y = sys.modules['x'].y

(except of course you don't have to have imported sys, and it will load the
module 'x' if it hasn't already been imported.)
>b.py ----------------------

from a import foo

In other words:

foo = a.foo

foo in module b is initialised from a.foo, but it is a separate variable.
So when a.foo is rebound that doesn't affect b.foo.
>def b(): print foo

c.py ----------------------

import a
from b import b

and here:
b = b.b
>print 'per a.a() ',a.foo
a.a(245)
print 'expect 245 ', a.foo
b()

Thanks, Duncan. It now makes sense.

Aug 28 '06 #3

Chaz Ginger wrote:
I am somewhat new to Python (last year). As such I encounter little
"gotchas" all the time. I am wondering is someone can explain this to me:

If have three simple files:

a.py ---------------------

foo = None
def a(b):
global foo
foo = b

b.py ----------------------

from a import foo
def b(): print foo

c.py ----------------------

import a
from b import b

print 'per a.a() ',a.foo
a.a(245)
print 'expect 245 ', a.foo
b()
If I run 'python c.py' I get the following printed out:
per a.a() None
expect 245 245
None
That surprised me. If I change b.py to

import a
def b(): print a.foo

I get the following (which is what I expected originally):
per a.a() None
expect 245 245
245
Can someone explain what is really going on here?
You are, in a very roundabout fashion, effectively executing the
following bindings:

a.foo = None # done when a is first imported
b.foo = a.foo # done in module b by "from a import foo"
a.foo = 245

So b.foo is bound to None, and a.foo is bound to 245.

Cheers,
John

Aug 28 '06 #4
John Machin wrote:
Chaz Ginger wrote:
>I am somewhat new to Python (last year). As such I encounter little
"gotchas" all the time. I am wondering is someone can explain this to me:

If have three simple files:

a.py ---------------------

foo = None
def a(b):
global foo
foo = b

b.py ----------------------

from a import foo
def b(): print foo

c.py ----------------------

import a
from b import b

print 'per a.a() ',a.foo
a.a(245)
print 'expect 245 ', a.foo
b()
If I run 'python c.py' I get the following printed out:
per a.a() None
expect 245 245
None
That surprised me. If I change b.py to

import a
def b(): print a.foo

I get the following (which is what I expected originally):
per a.a() None
expect 245 245
245
Can someone explain what is really going on here?

You are, in a very roundabout fashion, effectively executing the
following bindings:

a.foo = None # done when a is first imported
b.foo = a.foo # done in module b by "from a import foo"
a.foo = 245

So b.foo is bound to None, and a.foo is bound to 245.

Cheers,
John
Thanks John. It is a lot different from the C and C++ world where you
can hold a reference to something and use it. That is how I thought
about what I was doing and learned it wasn't quite right! lol.

Thanks again.
Aug 28 '06 #5

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

Similar topics

4
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
13
by: elad | last post by:
Hi The Menu doesn't work properly when I have 2 frame and the Menu popup frame=document target frame, when I choose item in the menu the doc opened and the menu get stuck. Here is the code...
24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
7
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into...
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
4
by: Peter Row | last post by:
Hi, I have created a UserControl which is subsequently hosted on a standard form. My control has a TabControl on it but it has no TabPages configured. At runtime I create X pages and put a...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.