|  | Someone enlightened me |  | |
| | | Marcus Low |  |
| Posted: Sun Jul 13, 2008 12:08 am Post subject: Someone enlightened me |  |
Can someone explain to me, why the behaviour below is different when u remark "lister" and unremark "self.lister"?
#-------------------------------------------------------------- class abc : # remark this later and unremark "self.lister" lister = []
def __init__ (self, val): #self.lister = [] self.lister.append(val)
#-------------------------------------------------------------- globallist = [] #-------------------------------------------------------------- def test () : global l for x in range(10) : o = abc(x) globallist.append(o) o = "" for i in globallist : print i.lister
#-------------------------------------------------------------- test() #-------------------------------------------------------------- |
| |
| | | Marcus Low |  |
| Posted: Sun Jul 13, 2008 12:14 am Post subject: Re: Someone enlightened me |  |
Duh,
Ok here is the file again ...attached. I give up doing this via the email editor. Sorry! new to the mailing list.
Marcus
Marcus Low wrote:
| Quote: | Opps here is the mail again, incase my formatting is lost, can someone explain to me why this code behaves differently when "lister" and "self.lister" is swap remarked.
class abc : # remark this later and unremark "self.lister" lister = [] def __init__ (self, val): #self.lister = [] self.lister.append(val) globallist = []
def test () :
global l for x in range(10) : o = abc(x) globallist.append(o) o = "" for i in globallist : print i.lister test()
|
|
| |
| | | bukzor |  |
| Posted: Sun Jul 13, 2008 3:03 am Post subject: Re: Someone enlightened me |  |
| |  | |
On Jul 12, 7:08 pm, Marcus Low <mar...@internetnowasp.net> wrote:
| Quote: | Can someone explain to me, why the behaviour below is different when u remark "lister" and unremark "self.lister"?
#-------------------------------------------------------------- class abc : # remark this later and unremark "self.lister" lister = []
def __init__ (self, val): #self.lister = [] self.lister.append(val)
#-------------------------------------------------------------- globallist = [] #-------------------------------------------------------------- def test () : global l for x in range(10) : o = abc(x) globallist.append(o) o = ""
for i in globallist : print i.lister
#-------------------------------------------------------------- test() #--------------------------------------------------------------
|
The way it's written, you're appending to a list associated with the class itself, which is created only once, then printing out that list 10 times. After you uncomment and comment the specified lines (this is the usual term, rather than "remark"), you are using a list that is associated with the actual object, then printing out the 10 different lists.
Hope that's clear enough. --Buck |
| |
| | | Gary Herron |  |
| Posted: Sun Jul 13, 2008 1:23 pm Post subject: Re: Someone enlightened me |  |
Marcus Low wrote:
| Quote: | Opps here is the mail again, incase my formatting is lost, can someone explain to me why this code behaves differently when "lister" and "self.lister" is swap remarked.
class abc : # remark this later and unremark "self.lister" lister = [] def __init__ (self, val): #self.lister = [] self.lister.append(val) globallist = []
def test () :
global l for x in range(10) : o = abc(x) globallist.append(o) o = "" for i in globallist : print i.lister test()
-- LINK
|
It's a Python scoping rule:
If a variable is assigned to anywhere within a function, it is assumed to be local *everywhere* within that function.
See the faq for more: LINK
Gary Herron |
| |
|
|