Python scoping

G

gervaz

Hi all, can you explain me why this simple function works well (i.e. I
can call the print function using txt) in py
.... 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
 
C

Chris Angelico

Hi all, can you explain me why this simple function works well (i.e. I
can call the print function using txt) in py

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

Chris Angelico

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
 
S

Steven D'Aprano

Hi all, can you explain me why this simple function works well (i.e. I
can call the print function using txt) in py

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

Chris Angelico

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
 
G

gervaz

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top