Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » Python

Someone enlightened me

 
Jump to:  
 
Marcus Low
PostPosted: 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
PostPosted: 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
PostPosted: 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
PostPosted: 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
 

Page 1 of 1 .:.

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates

ubezpieczenie emerytalne Bielizna damska Łódzkie Karpacz noclegi poker zasady