473,414 Members | 1,729 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,414 software developers and data experts.

module variable scope

I want to set a modules global variable from outside like this:

<begin a.py>
v = None

def setv( x ): v = x
def getv(): return v
<end a.py>

<begin b.py>
from a import v, setv, getv

print v
setv( 'hello' )
print print getv()
<end b.py>
Running b.py gives:
None
None

I expected variable v to be changed by calling setv() but it is not.
Who can explain this?

Thanks
Stefan
Jul 18 '05 #1
3 8904
Stefan Quandt wrote:
...
<begin b.py>
from a import v, setv, getv
The from statement takes a "snapshot" at that instant of time of
the values currently bound to those names in the other module,
binding each value to the same name in the current module.

That's all. It establishes no magical "persistent linkage
between names in different modules" -- there exists no concept
of such magical persistent linkage in Python.

Any rebinding of those or other names in the other module in
the future will be totally irrelevant to the binding of said
names in the current module.

I expected variable v to be changed by calling setv() but it is not.
Who can explain this?


Who can explain your expectations, I dunno. I hope the
explanation of the behavior was clear.

My often-repeated recommendation is: forget the from statement's
very existence. Use

import b

and refer to b.v -- NOT to bare v -- and THEN everything will work
according to expectations.

The 'from' statement is occasionally a handy shortcut if you
know what you're doing, but it doesn't work well when you're
not sure what you're doing, nor when any name rebinding or
module reloading may be involved. That's three strikes
against it, which means _it's out_:-).

The 'import' statement has basically no counter-indications.
If a module's name is long so it gets weary writing
theothermodule.v all the time, use the 'as' clause of import:

import theothermodule as t

and happily refer to t.v -- just 2 characters longer than
barename v, AND with other advantages (immediate clarity of
where the name comes from) as well as avoiding surprises.
Alex

Jul 18 '05 #2
Stefan Quandt fed this fish to the penguins on Tuesday 28 October 2003
05:37 am:


I want to set a modules global variable from outside like this:

<begin a.py>
v = None

def setv( x ): v = x
Problem #1... this /v/ is LOCAL to setv(). You need

def setv(x):
global v
v = x
def getv(): return v
<end a.py>

<begin b.py>
from a import v, setv, getv
Problem #2... from <> import <> is /evil/

What you have done is make new /local/ labels for the objects named.

Fixing problem #1 will result in the label "a.v" being moved to the
object containing "x". NOTE: the LABEL moved... "b.v" is not moved,
only "a.v".

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Bestiaria Home Page: http://www.beastie.dm.net/ <
Home Page: http://www.dm.net/~wulfraed/ <


Jul 18 '05 #3
Alex Martelli <al***@aleax.it> wrote in message news:<8t***********************@news2.tin.it>...
Stefan Quandt wrote:
...
<begin b.py>
from a import v, setv, getv


The from statement takes a "snapshot" at that instant of time of
the values currently bound to those names in the other module,
binding each value to the same name in the current module.

That's all. It establishes no magical "persistent linkage
between names in different modules" -- there exists no concept
of such magical persistent linkage in Python.

Any rebinding of those or other names in the other module in
the future will be totally irrelevant to the binding of said
names in the current module.

Oh, I thought that 'from' is a means to explicitly import selected
symbols from a module instead of importing the whole.
I never was aware of the different semantics of using 'from' in
imports.
In this case python does not work like one would intuitively expect.

Of course one could find a hint in the reference manual 6.12:
The from form does not bind the module name: it goes through the list
of identifiers, looks each one of them up in the module found in step
(1), and binds the name in the local namespace to the object thus
found.

Thanks
Stefan
Jul 18 '05 #4

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

Similar topics

59
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
2
by: TNGgroup | last post by:
Hi, a Simple Question, I have some code at my form, the code in the form is calling a certain module and execute some code, a certain variable is set to strTemp. but when de module is done. I...
2
by: Anand Sagar | last post by:
If I declare a variable in a .vb module file , like Public Module modMain Public temp As String End Module I am able to access the variable temp in all the pages of my site. However, I want...
1
by: Just Me | last post by:
Given a file containing Multiple classes and a variable that needs to be seen in all the classes I can put it in one of the classes as Shared and reference it the other classes as ...
3
by: Sam | last post by:
Hi all What's standard naming convention for module and global variables in .net? Do we still use lower case prefix "m" for module (or class) scope and "g" for global scope? Regards, Sam
8
by: MakaMaka | last post by:
Hi, I have a scope related question that I haven't been able to find an answer to anywhere. Is there a way to have a function in an imported module add variables to the scope of the calling...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
4
by: rshepard | last post by:
I'm stymied by what should be a simple Python task: accessing the value of a variable assigned in one module from within a second module. I wonder if someone here can help clarify my thinking. I've...
0
by: Fredrik Lundh | last post by:
Jeff Dyke wrote: so how did that processing use the "mymodulename" name? the calling method has nothing to do with what's considered to be a local variable in the method being called, so...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.