python newbie
J. Clifford Dyer
jcd at sdf.lonestar.org
Fri Nov 2 11:45:45 EDT 2007
On Fri, Nov 02, 2007 at 11:13:00AM -0400, Jim Hendricks wrote regarding Re: python newbie:
>
> BartlebyScrivener wrote:
> > On Nov 2, 8:51 am, Jim Hendricks <j... at bizcomputinginc.com> wrote:
> >> New to python, programming in 15 or so langs for 24 years.
> >>
> >> Couple of questions the tuts I've looked at don't explain:
> >
> > Did you look at THE tut? You would seem to be the perfect reader for
> > it, because you are already a programmer.
> >
> > http://docs.python.org/tut/node6.html#SECTION006600000000000000000
> >
> > rd
> >
>
> I initially looked at THE tut, and it came across so techy that it's
> such a slow read. Therefore, I looked to other tuts that were very easy
> reads and could give me the basic lowdown of the language. Problem of
> course is the easy read tuts don't get into the details.
>
> That said, I looked at the section of the tutorial you provided (thanks)
> and I am still confused. It specifies a global var cannot be assigned
> unless it's specified in the global statement.
>
> Here's an example of what I am asking:
>
> def my_function():
> global x
>
> x = open( ....
>
> before calling my_function, x does not exist in my program. So, my
> question is in my_function, the combination of using the global
> statement, then implicitly creating x via assignment to the result of
> the open function, is x created in the global namespace?
Yes.
>>> y
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
y
NameError: name 'y' is not defined
>>> def f():
global y
y = 123
>>> f()
>>> y
123
>>>
Remember, the interactive interpreter is your friend. Just type 'python' at the command line and hammer away. (It's even better through a decent IDE).
Oh yeah, you have to actually call f before y will get defined.
Cheers,
Cliff
More information about the Python-list
mailing list