why this simple code doesn't work

This is the place for queries that don't fit in any of the other categories.

why this simple code doesn't work

Postby tianchen » Sat Dec 28, 2013 9:13 pm

Hi everyone,
I have writen an extremely simple module to test built-in functions but the globals( ) just fails to work

Code: Select all
global y
y=12

print globals()

Money = 2000
def AddMoney():
   global Money
   Money = Money + 1
   print Money
   print globals()


if __name__=="__main__":
    AddMoney()



Code: Select all
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 540, in runfile
    execfile(filename, namespace)
  File "E:/Program Files/PyderWorkspace/Examples/a.py", line 11, in <module>
    print globals()
TypeError: 'module' object is not callable


why does it continously give me "module' object is not callable" error? Thanks for a hint :)
Last edited by tianchen on Sat Dec 28, 2013 9:52 pm, edited 1 time in total.
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: why this simple code doesn't work

Postby stranac » Sat Dec 28, 2013 10:11 pm

The code works fine for me.
My guess would be it has something to do with your ide.

Try printing globals (without parens), to see what your code thinks globals is.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 880
Joined: Thu Feb 07, 2013 3:42 pm

Re: why this simple code doesn't work

Postby micseydel » Sun Dec 29, 2013 1:43 am

I think stranac's right, and if so, BOOOO on that IDE for overwriting built-in variables!
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 923
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: why this simple code doesn't work

Postby tianchen » Sun Dec 29, 2013 4:22 pm

stranac wrote:The code works fine for me.
My guess would be it has something to do with your ide.

Try printing globals (without parens), to see what your code thinks globals is.


You are right. I try to run it again today, it works. And I ran another "real codes" with globals( ) which never works and come back to this toy code again. It gives me the same error again. But I close the spyder(the IDE I use) and run my toy code, it works again. It drives me crazy. The globals( ) in "real code" just never work.

Basically I have done nothing in between.
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: why this simple code doesn't work

Postby tianchen » Sun Dec 29, 2013 4:53 pm

stranac wrote:The code works fine for me.
My guess would be it has something to do with your ide.

Try printing globals (without parens), to see what your code thinks globals is.


I have printed globals and figured it out that I have a module with the same name as globals in another projects. Thank you a lot again.
Can I ask you another question?
I have two scripts. one is for defining global variables called myGlobals.py. In it
Code: Select all
def __init__():
    global myList
    myList = ["two"]


and another called main.py try to load global variable myList from myGlobals .


Code: Select all
import myGlobals
myGlobals.__init__()

print globals()["myList"]



Is there something wrong with this mechanism? Because the index in global variable dictionary( globals()["myList"] ) always give "Error Key" .
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm

Re: why this simple code doesn't work

Postby stranac » Sun Dec 29, 2013 5:12 pm

The correct way to do that would be not doing it at all. It's a terrible idea.
Globals in general are rarely useful.
Why do you think you want something like that?

Also, there is no need to make duplicate posts to answer to different people.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 880
Joined: Thu Feb 07, 2013 3:42 pm

Re: why this simple code doesn't work

Postby tianchen » Sun Dec 29, 2013 6:46 pm

stranac wrote:The correct way to do that would be not doing it at all. It's a terrible idea.
Globals in general are rarely useful.
Why do you think you want something like that?

Also, there is no need to make duplicate posts to answer to different people.


hi stranac. Yes you are right. I have copied the question twice because I think that might give me a quicker solution . Sorry for that first.

I have done this because I have some variables that should be used though the whole projects in every modules. I do not know what's the variant way of handling this.And also what's wrong with this. Thanks
tianchen
 
Posts: 11
Joined: Fri Dec 27, 2013 2:29 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests