which a is used?

J

Jayden

Dear All,

I have a simple code as follows:

# Begin
a = 1

def f():
print a

def g():
a = 20
f()

g()
#End

I think the results should be 20, but it is 1. Would you please tell me why?

Thanks a lot!
 
D

Dwight Hutto

Dear All,

I have a simple code as follows:

# Begin
a = 1

def f():
print a

def g():
a = 20
f()

g()
#End

I think the results should be 20, but it is 1. Would you please tell me why?

Thanks a lot!

didn't return the value, or print it out
 
D

Dwight Hutto

this prints a from calling f() function

call print a wasn't in a class where it was self.a and changed, so it
performed the function f which prints a and a = 1, but g just assigns
the 20, and calls the f() function which shows a, there is no self.a,
nor a class to show this in.
 
A

alex23

Dear All,

I have a simple code as follows:

# Begin
a = 1

def f():
    print a

def g():
    a = 20
    f()

g()
#End

I think the results should be 20, but it is 1. Would you please tell me why?

Because you don't declare 'a' in 'f', it looks for 'a' in the
surrounding scope _where it was defined_, not where it was called.
Because 'a' in the module scope is 1, you'll get 1.
 
S

Steven D'Aprano

Dear All,

I have a simple code as follows:

# Begin
a = 1

def f():
print a

def g():
a = 20
f()

g()
#End

I think the results should be 20, but it is 1. Would you please tell me
why?

You are expecting "dynamic scoping", Python uses "static scoping" (or
lexical scoping). With lexical scoping, you can reason about the
behaviour of a function by knowing only how and where it is defined. The
caller is irrelevant.

Since function f is defined globally, and does not have its own local
variable a, it will always see the global variable a no matter where it
is called. So when you call f() from inside g(), f prints 1, the global
a, not 20, g's local a.

While dynamic scoping has its uses, it is more tricky to use correctly.
One can no longer understand the behaviour of a function just by reading
the function's own code, knowing where and how it is defined. You also
need to know where it is called. A function f that works perfectly when
you call it from functions g, h, k, ... will suddenly misbehave (crash,
or worse, behave wrongly) when called from function v because v
accidentally changes some global variable that f relies on.

This is especially a danger for Python, because built-in functions like
len, chr, ord, print (version 3 only), and many others are all global
variables.

(Technically, they are in a third scope, the builtins, but that's
equivalent to being global.)
 
D

Dwight Hutto

You are expecting "dynamic scoping", Python uses "static scoping" (or
lexical scoping). With lexical scoping, you can reason about the
behavioPaul Rubin <[email protected]>ur of a function by knowing only how and where it is defined. The
caller is irrelevant.

Since fuPaul Rubin <[email protected]>nction f is defined globally, and does not have its own local
variable a, it will always see the global variable a no matter where it
is called. So when you call f() from inside g(), f prints 1, the global
a, not 20, g's local a.

While dynamic scoping has its uses, it is more tricky to use correctly.
One can no longer understand the behaviour of a function just by reading
the funcPaul Rubin <[email protected]>tion's own code, knowing where and how it is defined. You also
need to know where it is called. A function f that works perfectly when
you call it from functions g, h, k, ... will suddenly misbehave (crash,
or worse, behave wrongly) when called from function v because v
accidentally changes some global variable that f relies on.

This is especially a danger for Python, because built-in functions like
len, chr, ord, print (version 3 only), and many others are all global
variables.

(Technically, they are in a third scope, the builtins, but that's
equivalent to being global.)

But within a class this is could be defined as self.x within the
functions and changed, correct?


class a():
def __init__(self,a):
self.a = a

def f(self):
print self.a

def g(self):
self.a = 20
print self.a


a = a(1)
a.f()
a.g()

 
D

Dwight Hutto

But within a class this is could be defined as self.x within the
functions and changed, correct?


class a():
def __init__(self,a):
self.a = a

def f(self):
print self.a

def g(self):
self.a = 20
print self.a


a = a(1)
a.f()
a.g()


Yielding:

david@david-desktop:~$ python answer_to_email.py
1
20
david@david-desktop:~$
 
A

alex23


I honestly could not care less what you think about me, but don't use
that term. This isn't a boys' club and we don't need your hurt ego
driving people away from here.
 
D

Dwight Hutto

I honestly could not care less what you think about me, but don't use
that term. This isn't a boys' club and we don't need your hurt ego
driving people away from here.

OOOOOOOOOHHHHH. stirrin up shit and can't stand the smell. Turn and
switch technique. "You're so vulgar, and I wasn't."Go get a clue ho,
and then you can gumshoe it up the last crack alley on the left, and
suck till my nut hair tickles your tonsils.



 
T

Terry Reedy

Revising my answer to your other post.

Anything else bitch, take time to think about it.

This is completely bizarre, and uncalled for as an apparent response to
Alex. Your next response is too dirty to read, let alone quote. Please
desist. If necessary, learn to wait a few minutes before hitting send.
 
T

Thomas Rachel

Am 25.09.2012 03:47 schrieb Dwight Hutto:
But within a class this is could be defined as self.x within the
functions and changed, correct?


class a():
def __init__(self,a):
self.a = a

def f(self):
print self.a

def g(self):
self.a = 20
print self.a


a = a(1)
a.f()
a.g()

Yes - this is a different situation. Here, the "self" referred to is the
same in all cases (the "a" from top level), and so self.a can be used
consistently as well.


Thomas
 
T

Thomas Rachel

Am 25.09.2012 03:13 schrieb Dwight Hutto:
Anything else bitch, take time to think about it.

And you wonder if people don't like you because of your language?


Thomas
 
D

Dwight Hutto

Am 25.09.2012 03:13 schrieb Dwight Hutto:




And you wonder if people don't like you because of your language?

No, not really. If you wanna talk shit, I can reflect that, and if you
wanna talk politely I can reflect that. I go t attacked first., and
project managers don't get shoved around, they listen, respond, and if
wrong correct themselves, if not, they slam back , or their position
gets taken.
 
D

Dwight Hutto

OOOOOOOOOHHHHH. stirrin up shit and can't stand the smell.
Where did he so?


You'd have to read the other posts. And remember that some of these
names are A.K.A.'s, they ask respond, and befriend another name
through another proxy.
 
T

Thomas Rachel

Am 25.09.2012 07:22 schrieb Dwight Hutto:
No, not really. If you wanna talk shit, I can reflect that, and if you
wanna talk politely I can reflect that. I go t attacked first.,

But not in this thread.

Some people read only selectively and see only your verbal assaults,
without noticing that they refer to.

If you was really insulted, you should answer to these insults in their
thread and not in a different one.


Thomas
 
M

Mark Lawrence

Am 25.09.2012 04:37 schrieb Dwight Hutto:

Where did he so?


Thomas

He's referring to threads on the tutor mailing list where I have
repeatedly asking him to provide context when he replies. Unfortunately
he doesn't seem to understand the term context so resorts to attacking
me. In a part of one thread he referred to my family as pigs. I've
have lived with that, using the sticks and stones reply, but then
someone had the audacity to protect his stance. I am sure that people
have seen enough of his behaviour in the last few hours to see the real
Dwight Hutto so I'll leave it at that.
 
A

Alain Ketterlin

Jayden said:
# Begin
a = 1

def f():
print a

def g():
a = 20
f()

g()
#End

I think the results should be 20, but it is 1. Would you please tell me why?

When python looks at g(), it sees that a variable a is assigned to, and
decides it is a local variable. When it looks at f(), it sees a use of a
but no assignment, so it decides it is a global variable and fetches the
value from the outer scope.

If you change f() to:

def f():
print a
a = 30

you change a into a local variable (and get another error).

If you want to change the binding of a in g(), you can declare it
global:

def g():
global a
a = 20
f()

Very tricky, actually.

-- Alain.
 
A

alex23

You'd have to read the other posts. And remember that some of these
names are A.K.A.'s, they ask respond, and befriend another name
through another proxy.

You've actively accused me of this several times. If you have evidence
that there's sockpuppeting, please provide it.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top