Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8037
| References | <2f69dda9-9532-4fd1-af06-ba85cc2a0e33@v5g2000yqn.googlegroups.com> |
|---|---|
| Date | 2011-06-21 08:52 +1000 |
| Subject | Re: Python scoping |
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.200.1308610345.1164.python-list@python.org> (permalink) |
On Tue, Jun 21, 2011 at 8:35 AM, gervaz <gervaz@gmail.com> wrote: > Hi all, can you explain me why this simple function works well (i.e. I > can call the print function using txt) in py > >>>> def test(value): > ... if value%5: txt = "hello" > ... else: txt = "test" > ... print(txt) It's as though you had "PyObject txt;" at the top of the function. The scope is the function. There's no way (afaik) to make a variable be local to a portion of the function - that's a feature that has to be sacrificed to the simplicity of not declaring variables. In my opinion it's better to declare them, except in interactive code (eg IDLE or just typing "python"). But Python isn't that. Chris Angelico
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Python scoping gervaz <gervaz@gmail.com> - 2011-06-20 15:35 -0700
Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 08:52 +1000
Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 10:39 +1000
Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 10:55 +1000
Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 12:38 +1000
Re: Python scoping Chris Angelico <rosuav@gmail.com> - 2011-06-21 13:21 +1000
Re: Python scoping Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 14:06 +1000
Re: Python scoping gervaz <gervaz@gmail.com> - 2011-06-21 02:05 -0700
Re: Python scoping Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-06-21 00:57 +0000
csiph-web