Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8035 > unrolled thread

Python scoping

Started bygervaz <gervaz@gmail.com>
First post2011-06-20 15:35 -0700
Last post2011-06-21 00:57 +0000
Articles 9 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  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

#8035 — Python scoping

Fromgervaz <gervaz@gmail.com>
Date2011-06-20 15:35 -0700
SubjectPython scoping
Message-ID<2f69dda9-9532-4fd1-af06-ba85cc2a0e33@v5g2000yqn.googlegroups.com>
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?
Is there a way to force the scoping?


Thanks,

Mattia

[toc] | [next] | [standalone]


#8037

FromChris Angelico <rosuav@gmail.com>
Date2011-06-21 08:52 +1000
Message-ID<mailman.200.1308610345.1164.python-list@python.org>
In reply to#8035
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

[toc] | [prev] | [next] | [standalone]


#8043

FromBen Finney <ben+python@benfinney.id.au>
Date2011-06-21 10:39 +1000
Message-ID<87ei2oqaqe.fsf@benfinney.id.au>
In reply to#8035
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

[toc] | [prev] | [next] | [standalone]


#8046

FromChris Angelico <rosuav@gmail.com>
Date2011-06-21 10:55 +1000
Message-ID<mailman.205.1308617759.1164.python-list@python.org>
In reply to#8043
On Tue, Jun 21, 2011 at 10:39 AM, Ben Finney <ben+python@benfinney.id.au> wrote:
> gervaz <gervaz@gmail.com> writes:
> 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.

But variable names in C and variable names in Python follow fairly
similar rules. Yes, there's the whole thing of automatic sharing and
automatic deallocation, but the name still follows rules of scoping
that are very similar - but more flexible in C.

> 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.

It does have a type. It's a Python object. That data type can hold any
one "thing". :)

ChrisA

[toc] | [prev] | [next] | [standalone]


#8057

FromBen Finney <ben+python@benfinney.id.au>
Date2011-06-21 12:38 +1000
Message-ID<87aadbrjrh.fsf@benfinney.id.au>
In reply to#8046
Chris Angelico <rosuav@gmail.com> writes:

> On Tue, Jun 21, 2011 at 10:39 AM, Ben Finney <ben+python@benfinney.id.au> wrote:

> > 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.
>
> But variable names in C and variable names in Python follow fairly
> similar rules.

The names are similar, yes. But names are not variables, and the
conceptual baggage of the term “variable” simply doesn't apply to a
Python identifier.

> Yes, there's the whole thing of automatic sharing and automatic
> deallocation, but the name still follows rules of scoping that are
> very similar - but more flexible in C.

The *binding* is scoped.

> > 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.
>
> It does have a type. It's a Python object.

No, a name is not a Python object, and has no type. A name is simply an
identifier.

> That data type can hold any one "thing". :)

A name refers to an object, it doesn't “hold” anything.

-- 
 \        “… it's best to confuse only one issue at a time.” —Brian W. |
  `\    Kernighan and Dennis M. Ritchie, _The C programming language_, |
_o__)                                                             1988 |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#8062

FromChris Angelico <rosuav@gmail.com>
Date2011-06-21 13:21 +1000
Message-ID<mailman.212.1308626481.1164.python-list@python.org>
In reply to#8057
On Tue, Jun 21, 2011 at 12:38 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> The *binding* is scoped.
>

And the binding follows the exact same rules as anything else would.
It has scope and visibility. In terms of the OP, the binding IS like a
variable.

ChrisA

[toc] | [prev] | [next] | [standalone]


#8067

FromBen Finney <ben+python@benfinney.id.au>
Date2011-06-21 14:06 +1000
Message-ID<87tybjq157.fsf@benfinney.id.au>
In reply to#8062
Chris Angelico <rosuav@gmail.com> writes:

> On Tue, Jun 21, 2011 at 12:38 PM, Ben Finney <ben+python@benfinney.id.au> wrote:
> > The *binding* is scoped.
>
> And the binding follows the exact same rules as anything else would.
> It has scope and visibility. In terms of the OP, the binding IS like a
> variable.

Yes. So let's stop behaving as though the *name* behaves like a
variable. It isn't, and doesn't.

-- 
 \          “Computer perspective on Moore's Law: Human effort becomes |
  `\           twice as expensive roughly every two years.” —anonymous |
_o__)                                                                  |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#8079

Fromgervaz <gervaz@gmail.com>
Date2011-06-21 02:05 -0700
Message-ID<f784714d-2061-462d-9dac-be608ba77c2f@u28g2000yqf.googlegroups.com>
In reply to#8067
On 21 Giu, 06:06, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> Chris Angelico <ros...@gmail.com> writes:
> > On Tue, Jun 21, 2011 at 12:38 PM, Ben Finney <ben+pyt...@benfinney.id.au> wrote:
> > > The *binding* is scoped.
>
> > And the binding follows the exact same rules as anything else would.
> > It has scope and visibility. In terms of the OP, the binding IS like a
> > variable.
>
> Yes. So let's stop behaving as though the *name* behaves like a
> variable. It isn't, and doesn't.
>
> --
>  \          “Computer perspective on Moore's Law: Human effort becomes |
>   `\           twice as expensive roughly every two years.” —anonymous |
> _o__)                                                                  |
> Ben Finney

Ok, thanks for the clarification, I'll take extra care e.g. when
dealing with exceptions.

Ciao,

Mattia

[toc] | [prev] | [next] | [standalone]


#8047

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2011-06-21 00:57 +0000
Message-ID<4dffec80$0$30002$c3e8da3$5496439d@news.astraweb.com>
In reply to#8035
On Mon, 20 Jun 2011 15:35:35 -0700, gervaz 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)
> 
> while in other languages like C the txt identifier would be undefined?

Because Python is not C and doesn't require declarations.


> Is there a way to force the scoping?

It is scoped. txt is local to the function test. What more do you want?

If your functions are so large that you worry about name clashes between 
code in different parts of the one function, the solution is to break 
this one huge function into many small functions. That's what functions 
are for: to encapsulate a limited amount of reusable functionality into a 
namespace.



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web