473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Objects, lists and assigning values


Hello,

while trying to learn how to program using objects in python (up to now
simple scripts were sufficient for my needs) I stumbled over the
a problem while assigning values to an object.

The following piece of code shows what I intend to do:

<---snip--->

class new_class(object):
def __init__( self,
internal_list=[]):
self.internal_list= internal_list

external_list=[[b*a for b in xrange(1,5)] for a in xrange(1,5)]
print external_list

first_collection=[new_class() for i in xrange(4)]

temporary_list=[[] for i in xrange(4)]
for i in xrange(4):
for j in xrange(4):
temporary_list[i].append(external_list[i][j])
first_collection[i].internal_list=temporary_list[i]
#Now everything is as I want it to be:
for i in xrange(4):
print first_collection[i].internal_list
#Now I tried to get the same result without the temporary
#variable:

second_collection=[new_class() for i in xrange(4)]

for i in xrange(4):
for j in xrange(4):
second_collection[i].internal_list.append(external_list[i][j])

#Which obviously leads to a very different result:

for i in xrange(4):
print second_collection[i].internal_list

<---snip--->

Can someone explain to me, what's happening here and why the two
approaches do not lead to the same results? Thanks in Advance.

Regards,

Manuel


--
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
Apr 5 '07 #1
5 1629
En Thu, 05 Apr 2007 14:13:43 -0300, Manuel Graune <ma***********@koeln.de>
escribió:
class new_class(object):
def __init__( self,
internal_list=[]):
self.internal_list= internal_list
All your instances share the *same* internal list, because default
arguments are evaluated only once (when the function is defined)
The usual way is to write:

class new_class(object):
def __init__(self, internal_list=None):
if internal_list is None:
internal_list = []
self.internal_list= internal_list

See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm

--
Gabriel Genellina

Apr 5 '07 #2

Hello Gabriel, hello William,

thanks to both of you for your answers. I seem to need a
better book about python.

Regards,

Manuel

"Gabriel Genellina" <ga*******@yahoo.com.arwrites:
class new_class(object):
def __init__(self, internal_list=None):
if internal_list is None:
internal_list = []
self.internal_list= internal_list

See
http://effbot.org/pyfaq/why-are-defa...en-objects.htm

--
Gabriel Genellina
--
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
Apr 6 '07 #3
On Apr 6, 1:23 am, Manuel Graune <manuel.gra...@koeln.dewrote:
Hello Gabriel, hello William,

thanks to both of you for your answers. I seem to need a
better book about python.
What book are you reading?

Apr 6 '07 #4
"7stud" <bb**********@yahoo.comwrites:
>
What book are you reading?
I worked my way through most of the online-docs. A bit to casual
obviously.

As printed desktop-reference I use a german book called
"Python ge-packt".
--
A hundred men did the rational thing. The sum of those rational choices was
called panic. Neal Stephenson -- System of the world
http://www.graune.org/GnuPG_pubkey.asc
Key fingerprint = 1E44 9CBD DEE4 9E07 5E0A 5828 5476 7E92 2DB4 3C99
Apr 7 '07 #5
On Apr 7, 2:52 pm, Manuel Graune <manuel.gra...@koeln.dewrote:
"7stud" <bbxx789_0...@yahoo.comwrites:
What book are you reading?

I worked my way through most of the online-docs. A bit to casual
obviously.
See the online tutorial's section on default function arguments here:

http://docs.python.org/tut/node6.htm...00000000000000

specifically the "Important Warning".

Apr 7 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
0
by: vanGogh | last post by:
I have generated classes based on an Xml schema file (xsd) using the XSD.exe tool. My goal is to: - write an application that can parse XML documents - fill objects (based on the generated...
30
by: Brian Elmegaard | last post by:
Hi, I am struggling to understand how to really appreciate object orientation. I guess these are FAQ's but I have not been able to find the answers. Maybe my problem is that my style and...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.