[Tutor] calling global in funtions.

Santosh Kumar rhce.san at gmail.com
Wed Feb 26 08:12:00 CET 2014


All,

Requirement : i want to call a variable assigned outside a function scope
anytime within the function. I read "global" is a way.

a) Case I looks fine.
b) Case II is bombing out.

Is this how it works or please correct me if i am wrong.

case I:

In [17]: a = 10

In [19]: def fun_local():
   ....:     global a
   ....:     print "the value of a is %d" %(a)
   ....:     a = 5
   ....:     print "the value of a is %d" %(a)
   ....:

In [20]: fun_local()
the value of a is 10
the value of a is 5


CASE II:

In [21]: a = 10

In [22]: def fun_local():
   ....:     a = 5
   ....:     print "the value of a is %d" %(a)
   ....:     global a
<input>:4: SyntaxWarning: name 'a' is assigned to before global declaration
<input>:4: SyntaxWarning: name 'a' is assigned to before global declaration
<input>:4: SyntaxWarning: name 'a' is assigned to before global declaration



Thanks,
santosh
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140226/d0d89668/attachment.html>


More information about the Tutor mailing list