473,397 Members | 2,028 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,397 software developers and data experts.

unexpected class behaviour

Hi all,

can anyone explain the behaviour of the following code sniplet:

---schnipp <---
class Base(object):
def __init__( self, lst=[] ):
self.varlist = lst

def addVar( self, var ):
self.varlist.append(var)

class Derived(Base):
def __init__( self, var ):
Base.__init__(self)
self.addVar(var)

vars = ['foo', 'bar']

for ivar in vars:
obj = Derived(ivar)
print ivar, obj, obj.varlist
---schnapp <---

After running (Python 2.5.1), I get the following output:
foo <__main__.Derived object at 0xb7c608cc['foo']
bar <__main__.Derived object at 0xb7c6092c['foo', 'bar']

So, I get two different objects, but how does the 'foo' get into the second
varlist? I'm a little bit confused about this, any ideas?

Thanks in advance

Jan
Sep 7 '08 #1
2 1008
Jan Schäfer wrote:
can anyone explain the behaviour of the following code sniplet:
well, it *is* explained in the tutorial, the language reference, and the
FAQ, so yes, it can be explained ;-)

for more information, see this page:

http://effbot.org/zone/default-values.htm

</F>

Sep 7 '08 #2
On 7 sep, 12:40, Fredrik Lundh <fred...@pythonware.comwrote:
Jan Schäfer wrote:
can anyone explain the behaviour of the following code sniplet:

well, it *is* explained in the tutorial, the language reference, and the
FAQ, so yes, it can be explained ;-)

for more information, see this page:

http://effbot.org/zone/default-values.htm

</F>
Well, you may want replace the last line by:
print ivar, obj, obj.varlist, id(obj.varlist)

To have another behavior, you may want replace the 3 first lines by:
class Base(object):
def __init__( self, lst=None ):
if lst is None: lst=[]
self.varlist = lst

Enjoy Python !
Sep 7 '08 #3

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

Similar topics

2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
10
by: sindica | last post by:
I am using DevC++ 4.0 lately, which uses Mingw port of GCC, on a WinXP. I am surprised to see the malloc behaviour which is not consistent with the documentation. See the program and its output...
2
by: Diana Mueller | last post by:
Public Class Base Public Function Foo() As String Return MyClass.GetType.FullName End Function End Class Public Class Derrived Inherits Base End Class
9
by: Jeff Louie | last post by:
In C# (and C++/cli) the destructor will be called even if an exception is thrown in the constructor. IMHO, this is unexpected behavior that can lead to an invalid system state. So beware! ...
8
by: Steven D'Aprano | last post by:
I came across this unexpected behaviour of getattr for new style classes. Example: >>> class Parrot(object): .... thing = .... >>> getattr(Parrot, "thing") is Parrot.thing True >>>...
4
by: conan | last post by:
This regexp '<widget class=".*" id=".*">' works well with 'grep' for matching lines of the kind <widget class="GtkWindow" id="window1"> on a XML .glade file However that's not true for the...
4
by: Martin | last post by:
using VS2005 - VB.Net I noticed unexpected behaviour when using a combobox which has it's datasource set to a datatable. Also the Valuemember and Displaymember are being set. I'm getting a cast...
23
by: gu | last post by:
hi to all! after two days debugging my code, i've come to the point that the problem was caused by an unexpected behaviour of python. or by lack of some information about the program, of course!...
5
by: sukkopera | last post by:
Hi, I have just encountered a Python behaviour I wouldn't expect. Take the following code: ------------------------------------------------------------------------ class Parent: a = 1 def m...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.