By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,222 Members | 2,354 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,222 IT Pros & Developers. It's quick & easy.

Re: Two instances share an attribute

P: n/a
On 15Nov2008 22:41, Filip GruszczyƄski <gr******@gmail.comwrote:
| I really don't understand, what's happening with the following code.
| Am I doing something wrong?

Yes. This is a common mistake:

| class EnumeratedContent:
| def __init__(self, values = []):
| self.values_ = values

The "values = []" happens at class definition time, not instance
definition time. So when "values" is not supplied, the same list
is reused as the default value.

The usual idiom is this:

class EnumeratedContent:
def __init__(self, values = None):
if values is None:
values = []
self.values_ = values

which makes a new [] during the instance creation.

Cameron Simpson <

If you don't live on the edge, you're taking up too much space. - t-shirt
Nov 15 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.