Global var drives me crazy
#1
I found a strange problem with the following Python Code (Link above)

http://code.google.com/p/swiss-army-knif...default.py

On Line 73 I do create modul gloabl variable called __jobs__
and the value should be 0 on startup.


without the statement on line 217 the statement on line 283 fails.
The error-log says that I would use a local var but this is not true.

If I remove the 217 the code fails ....
Any hint ?

I allready use global variables inside the code

__enable_bluray__
__enable_network__
__enable_burning__
__enable_customer__

And I can use them without any problem ...

I did a try to insert the following statement (Line 217)

global __jobs__

No difference ... He sais allways the var __jobs__ is local ...
Reply
#2
Without knowing anything at all about python (well, a tiny bit :p ) perhaps it's because you're writing to the global - all the other globals you just read from?

Google agrees:

http://stackoverflow.com/questions/92977...-in-python

Cheers,
Jonathan
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#3
In the main-functions I do also setting the globals vars

__enable_bluray__
__enable_network__
__enable_burning__
__enable_customer__

And later in a secound function I do reference them...
Changed the type to boolan
And suprise ... suprise ..

With the global statement (global __jobs__) it does works, but why in hell do the other
vars that are also referenced work like expected without the global statement ?
Regards
Reply
#4
On none of the other variables are you setting them in a function, execpt for __jobs__.
Reply
#5
Yes that is correct but in the main function where I set the other vars I don't have
to put in the statement "global" to the function.
Reply
#6
That is because you are at the same level of programming. The __jobs__ are being set in the 'def's which are at a different level. When ever you set a variable in a 'def', even if you have it already declared outside, becomes local to that def unless it is declared as a global.
Reply
#7
I have it working by now :

http://code.google.com/p/swiss-army-knif...default.py

Please have a look to line 342-345
I do initial a couple of global vars without any global statement and it works well.
In the function GUIMain01Class I use the global var __jobs__ on line 316 only as
reference. In the same function I do also use the global vars from line 342-345 and
they are not statet as local.As soon I remove the line 250 it comes with a error that
the variable __jobs__ is used without assigment first.
I do not see the difference between __jobs_ and for example __enable_bluray__ on line
267 without the global definition.
Yes it works but I would like to know the reason of this behavior.
Reply
#8
as giftie said anything outside a class or def is global
For python coding questions first see http://mirrors.xbmc.org/docs/python-docs/
Reply

Logout Mark Read Team Forum Stats Members Help
Global var drives me crazy0