473,654 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(objec t):
def __init__( self,
internal_list=[]):
self.internal_l ist= internal_list

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

first_collectio n=[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(externa l_list[i][j])
first_collectio n[i].internal_list= temporary_list[i]
#Now everything is as I want it to be:
for i in xrange(4):
print first_collectio n[i].internal_list
#Now I tried to get the same result without the temporary
#variable:

second_collecti on=[new_class() for i in xrange(4)]

for i in xrange(4):
for j in xrange(4):
second_collecti on[i].internal_list. append(external _list[i][j])

#Which obviously leads to a very different result:

for i in xrange(4):
print second_collecti on[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 1654
En Thu, 05 Apr 2007 14:13:43 -0300, Manuel Graune <ma***********@ koeln.de>
escribió:
class new_class(objec t):
def __init__( self,
internal_list=[]):
self.internal_l ist= 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(objec t):
def __init__(self, internal_list=N one):
if internal_list is None:
internal_list = []
self.internal_l ist= 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*******@yaho o.com.arwrites:
class new_class(objec t):
def __init__(self, internal_list=N one):
if internal_list is None:
internal_list = []
self.internal_l ist= 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**********@y ahoo.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...@y ahoo.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
1590
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 classes) with the XML data in element nodes or attributes - assemble the objects so they are contained in their parent - add the resulting object to an arrayList The problem I’m having is that I am not sure how to go about assigning an object to...
0
281
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 classes) with the XML data in element nodes or attributes - assemble the objects so they are contained in their parent - add the resulting object to an arrayList The problem I’m having is that I am not sure how to go about assigning an object to...
30
1805
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 understanding are influenced by matlab and fortran. I tried with the simple example below and ran into several questions: 1: Why can't I do: def __init__(self,self.x):
55
3962
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 built-in-like behavior for objects of class type. That leads to the "necessity" for the exception machinery so that errors from constructors can be handled. Is all that complexity worth it just to get built-in-like behavior from class objects? Maybe a...
0
8294
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8816
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8709
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7309
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5627
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4297
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2719
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1924
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1597
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.