473,322 Members | 1,473 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,322 software developers and data experts.

What am I doing wrong?

I'm relatively new to python and I've run into this problem.
DECLARING CLASS

class structure:
def __init__(self, folders = []):
self.folders = folders

def add_folder(self, folder):
self.folders.append(tuple(folder))

Now I try to make an instance of this class

structure1 = structure()
structure1.add_folder([('foo'),])
print structure1.folders

This returns: [('foo',)]

This works fine. But when I try to make another instance of that class...

structure2 = structure()
print structure2.folders

This now also returns: [('foo',)]
Even though I haven't added any folders to this new instance

What am I doing wrong?

Sep 21 '05 #1
2 1286
You have been bitten by a well known "feature". You used
a mutable as default value in your argument list for __init__.

See:
http://www.nexedi.org/sections/educa...mutable_n/view
It would be better to write:

class structure:
def __init__(self, folders = None):
self.folders=folders or []

-Larry Bates
keithlackey wrote:
I'm relatively new to python and I've run into this problem.
DECLARING CLASS

class structure:
def __init__(self, folders = []):
self.folders = folders

def add_folder(self, folder):
self.folders.append(tuple(folder))

Now I try to make an instance of this class

structure1 = structure()
structure1.add_folder([('foo'),])
print structure1.folders

This returns: [('foo',)]

This works fine. But when I try to make another instance of that class...

structure2 = structure()
print structure2.folders

This now also returns: [('foo',)]
Even though I haven't added any folders to this new instance

What am I doing wrong?

Sep 21 '05 #2
keithlackey wrote:
I'm relatively new to python and I've run into this problem. This has two very standard mistakes:
First, as noted by Sybren, messages should just use spaces in order to
be readable.

After correcting that one: class structure:
def __init__(self, folders = []):
self.folders = folders
...


Here is the second one. Default args are not rebuilt, but shared.
The correct way to do this is:
class structure:
def __init__(self, folders=None):
if folders is None:
self.folders = []
else:
self.folders = folders
...

--Scott David Daniels
Sc***********@Acm.Org
Sep 21 '05 #3

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

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
2
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update...
16
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. ...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
0
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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: 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
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.