[Tutor] variables question.

Emile van Sebille emile at fenx.com
Wed Nov 10 22:08:16 CET 2010


On 11/10/2010 10:21 AM Jeff Honey said...
> I have a question about where variables are exposed in python.

You're looking for scoping rules -- see for example 
http://stackoverflow.com/questions/291978/short-description-of-python-scoping-rules 
where they get into some detail, but the short and older version is 1) 
search in local-global-builtin, and 2) if you assign to it in a scope, 
it's local to that scope.

So, blah and foo below are visible in all the functions except any that 
specifically assign to blah or foo.

HTH,

Emile


>
> I have a monolothic script with a number of functions defined, can those functions share variables? can I instantiate them outside the function of where they are needed? do they need to be wrapped in quotes, ever? For example:
>
> blah = 123
> foo = 'somestring'
>
> def function(foo):
>      code that needs foo
>      anotherfunction(blah)
>
> def anotherfunction(blah):
>      code that needs blah
>      code that uses foo
>
> ....how about this:
>
> def function(blah, foo):
>      anotherfunc(blah)
>      anotherfunc(foo)
>
> ...what about my python being called from some parent script (something OTHER than python) that instantiates blah and foo FOR me? Can I just plug those into my code like I would normally? I guess this is more about HOW and WHERE I can make variables available for use.
>
> --
>   ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
> ¤ kyoboku kazeoshi ¤
>   ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>




More information about the Tutor mailing list