2

I am new to python, and I am using a global variable to control my design easily, especially for debugging.

Here is my code:

hidNow = -1
def loginFromSql(br, n=0):
    global hidNow
    print hidNow
    hidNow = 5566
    print hidNow
print(hidNow)

And I run python directly with "from myModule import *"

Before calling the function, print hidNow get the result of -1, but it still -1 even the function is called....

Is it a bug in python? I am using python 2.7.5

Thanks!

5
  • 1
    @RobertSadler where is your opening tag!? Commented Oct 29, 2013 at 19:48
  • 6
    Please fix your indentation, your code is not readable. Commented Oct 29, 2013 at 19:48
  • Did you call the loginFromSql function? The assignment only happens when the function is called. Commented Oct 29, 2013 at 19:49
  • 1
    You defined loginFromSql but you never called it. You must call it for the global assignment to work. Commented Oct 29, 2013 at 19:56
  • Thanks for the comment, I update its indentation. And I am use python iteractive mode to import the module and call "loginFromSql" by myself Commented Oct 30, 2013 at 1:10

3 Answers 3

6

If you do from myModule import *, you create a variable called hidNow in the place where you do the import. This new variable is separate from the hidNow inside myModule. They have the same value initially, but assigning a new value to hidNow from inside myModule will not change the value you already imported.

One solution is don't use the * import. Instead do:

import myModule
print myModule.hidNow
myModule.loginFromSql()
print myModule.hidNow # will change

(This is what I think you're asking, but your question and code are unclear, so please edit and correct it if this isn't what you're getting at.)

You should think about why you'e using a global variable to store this information. Global variables are fragile.

Sign up to request clarification or add additional context in comments.

5 Comments

How about a mention of using a class to maintain state rather than a global variable
@wim: Baby steps, baby steps. . . :-)
OK I'll add another answer
@wim: I edited my answer to give a brief remark, but my point is that I don't think it's a great idea to go into details about why this is or isn't a good idea. At most I prefer to just say "here's how you do it, but by the way, think about not doing this at all".
That is a correct answer, I used to use "import myModule" format, but it bother me when I debug with the program. So I decide to use the "from myModule import *" to save my time to type prefix every time.
1

I am using a global variable to control my design easily, especially for debugging.

clippy

I'm going to gloss over the actual issue (as another answer has already pointed out the cause) and address the X part of this XY problem.

This is not really controlling your design easily like you may think, the use of global variables at the module level is usually not necessary and will appear to many python devs as a code smell.

A better way to maintain state is to use a class, which would go something like this:

# myModule.py
class Thing(object):

  def __init__(self, hidNow=-1):
    self.hidNow = hidNow

  def loginFromSql(self, br, n=0):
    self.hidNow = 5566

And here you see the access method for this attribute, and see the value change take place.

>>> from myModule import *
>>> my_thing = Thing()
>>> my_thing.hidNow
-1
>>> my_thing.loginFromSql('potato')
>>> my_thing.hidNow
5566

Comments

0

You defined loginFromSql but you did not call it. Here is your example script with a bit extra printing and a call to the function.

hidNow = -1
def loginFromSql(br, n=0):
    global hidNow
    print "login start", hidNow
    hidNow = 5566
    print "login done", hidNow

print "before call", hidNow
loginFromSql('x')
print "after call", hidNow

When you run it, you get this output, showing that the global does change

before call -1
login start -1
login done 5566
after call 5566

2 Comments

That is true if these codes are added to the file, but it is not true if these codes are directly typed in python interactive mode
@Yu-Chih, no they work the same in interactive mode. Try copy/paste into the interpreter.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.