Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8043
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!news-xfer.nntp.sonic.net!news.astraweb.com!border6.newsrouter.astraweb.com!not-for-mail |
|---|---|
| From | Ben Finney <ben+python@benfinney.id.au> |
| Newsgroups | comp.lang.python |
| Subject | Re: Python scoping |
| References | <2f69dda9-9532-4fd1-af06-ba85cc2a0e33@v5g2000yqn.googlegroups.com> |
| X-Public-Key-ID | 0xAC128405 |
| X-Public-Key-Fingerprint | 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 |
| X-Public-Key-URL | http://www.benfinney.id.au/contact/bfinney-pubkey.asc |
| X-Post-From | Ben Finney <bignose+hates-spam@benfinney.id.au> |
| Date | Tue, 21 Jun 2011 10:39:05 +1000 |
| Message-ID | <87ei2oqaqe.fsf@benfinney.id.au> (permalink) |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.3 (gnu/linux) |
| Cancel-Lock | sha1:+cThhxWV5QAHlcKWcM1PpvLrOsg= |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8 |
| Content-Transfer-Encoding | 8bit |
| Lines | 37 |
| Organization | Unlimited download news at news.astraweb.com |
| NNTP-Posting-Host | 01c19264.news.astraweb.com |
| X-Trace | DXC=?`1M_o:EZ@S]oQN>6;?:kYL?0kYOcDh@ZN7:H2`MmAUSG;jTe9??mMY]G;2>V^?kWSbEW9A[5UK?UNZ[SL`C\KgSM<>`k3c\E[^ |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8043 |
Show key headers only | View raw
gervaz <gervaz@gmail.com> writes: > 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) > > while in other languages like C the txt identifier would be undefined? Python doesn't have variables the way C or many other languages have them. Instead, Python has objects, and references to those objects so you can get at them. The Python documentation, much to my frustration, calls these references “variables” even though that gives exactly the wrong implication of how they'd behave. With the assignment statements (the statements using ‘txt = …’), the name ‘txt’ is bound as a reference to a value. It's not a C-like variable; it doesn't have a type, it doesn't need to be declared, etc. It's just a name, that you can bind to exactly one value any time you like. > Is there a way to force the scoping? No, by binding a name to a value you are creating that binding within the scope where the binding happens (the assignment statement, in your example). -- \ “Instead of a trap door, what about a trap window? The guy | `\ looks out it, and if he leans too far, he falls out. Wait. I | _o__) guess that's like a regular window.” —Jack Handey | Ben Finney
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