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

Setting Class Attributes

I have a small, simple class which contains a dictionary (and some
other stuff, not shown). I then have a container class (Big) that holds
some instances of the simple class. When I try to edit the elements of
the dictionary, all instances obtain those changes; I want each
instance to hold separate entries.

#----------Begin module test.py
class ex:
def __init__(self, val={}):
self.value = val
def __str__(self):
return str(self.value)

class Big:
def __init__(self):
self.A = ex()
self.B = ex()
def __str__(self)
return "A=%s, B=%s"%(self.A, self.B)
def changea(self):
a = self.A.value
a['x'] = 1
a['y'] = 10

print "x = Big()"
x = Big()
print x

print "x.changea()"
x.changea()
print x

print "x.B.value = 30"
x.B.value = 30
print x
#----------End module test.py

#----------Begin actual output
x = Big()
A={}, B={}
x.changea()
A={'x':1, 'y':10}, B={'x':1, 'y':10}
x.B.value = 30
A={'x':1, 'y':10}, B=30
#----------End actual output

#----------Begin Desired output
x = Big()
A={}, B={}
x.changea()
A={'x':1, 'y':10}, B={}
x.B.value = 30
A={'x':1, 'y':10}, B=30
#----------End Desired output

I'm never actually planning on changing the dictionary into something
else, I just included that test to see wether both A and B would be set
to 30. I'm clearly missing something here: Why do both dictionaries
appear to get the same data when they are clearly separate instances?

Oct 25 '05 #1
6 1723
the.theorist a écrit :
I have a small, simple class which contains a dictionary (and some
other stuff, not shown). I then have a container class (Big) that holds
some instances of the simple class. When I try to edit the elements of
the dictionary, all instances obtain those changes; I want each
instance to hold separate entries.

#----------Begin module test.py
class ex:
class ex(object): # oldstyle classes are deprecated
def __init__(self, val={}):
self.value = val


You didn't search very long. This is one of the most (in)famous Python
gotchas: default args are evaluated *only once*, when the function
definition is evaluated (at load time). This is also a dirty trick to
have a 'static' (as in C) like variable.

The solution is quite simple:
class ex(object):
def __init__(self, val=None):
if val is None: val = {}
self.value = val

(snip)
Oct 25 '05 #2

Bruno Desthuilliers wrote:
the.theorist a écrit :
I have a small, simple class which contains a dictionary (and some
other stuff, not shown). I then have a container class (Big) that holds
some instances of the simple class. When I try to edit the elements of
the dictionary, all instances obtain those changes; I want each
instance to hold separate entries.

#----------Begin module test.py
class ex:


class ex(object): # oldstyle classes are deprecated
def __init__(self, val={}):
self.value = val


You didn't search very long. This is one of the most (in)famous Python
gotchas: default args are evaluated *only once*, when the function
definition is evaluated (at load time). This is also a dirty trick to
have a 'static' (as in C) like variable.

The solution is quite simple:
class ex(object):
def __init__(self, val=None):
if val is None: val = {}
self.value = val

(snip)


Hey, that was extremely helpful. I suspect that the default args
evaluation is optimized for speed. So it makes sense to use the None
assignment, and a test condition later.

Worked like a charm, Thanks!

Oct 25 '05 #3
On 25 Oct 2005 15:57:41 -0700, "the.theorist" <th**********@gmail.com> wrote:

Bruno Desthuilliers wrote:
the.theorist a =E9crit :
> I have a small, simple class which contains a dictionary (and some
> other stuff, not shown). I then have a container class (Big) that holds
> some instances of the simple class. When I try to edit the elements of
> the dictionary, all instances obtain those changes; I want each
> instance to hold separate entries.
>
> #----------Begin module test.py
> class ex:
class ex(object): # oldstyle classes are deprecated
> def __init__(self, val=3D{}):
> self.value =3D val


You didn't search very long. This is one of the most (in)famous Python
gotchas: default args are evaluated *only once*, when the function
definition is evaluated (at load time). This is also a dirty trick to
have a 'static' (as in C) like variable.

The solution is quite simple:
class ex(object):
def __init__(self, val=3DNone):
if val is None: val =3D {}
self.value =3D val

(snip)


Hey, that was extremely helpful. I suspect that the default args
evaluation is optimized for speed. So it makes sense to use the None
assignment, and a test condition later.

That sounds like you missed the important point: The reason for using None
instead of {} as a default argument is that default arguments are only
evaluated when the function is defined, not when it's called, so if the
default value is {}, that very _same_ dict will be used as default for
the next call to __init__, so you will have every instance of ex looking
at the same val and binding it to its self.value, and then if one instance
modifies it (which it can, since a dict is mutable), the other instances
will see the modification in their self.value dicts. The None default
prevents re-use of a dict that wasn't actually passed in as an argument
replacing the default in the call. The call-time test for None discovers
that a fresh dict is needed, and self.value = {} creates that fresh dict
when __init__ executes, so the new instance gets its own separate self.value dict.

Nothing to do with optimization. In fact, re-using the shared default dict
would be faster, though of course generally wrong.
Worked like a charm, Thanks!

Just wanted to make sure you realize why it made a difference, in case ;-)

Regards,
Bengt Richter
Oct 26 '05 #4
So that it'll be easier to remember the next time I find myself in the
same situation on a different task, I'll extend the discussion
somewhat.

Coming from C, I had expected that I'd get a new empty dict every time
the __init__ function ran. Guido (or some other benevolent) had decided
to implement things a little bit differently in Python. I understand
that most everything is a pointer in Python. (which gives us cool stuff
like recursive data structures) So I was wondering, they could have
made the behavior C-like, but chose not to. The decision to bind
everything in the function to the same default args must be a
reflection of the Python Ideology. Philosophically, why was it done
this way, and how does it fit in with Python's design as a language.

(hopefully, reasons will help me remeber why things are the way they
are, so I don't forget in the future)
-----------------
I've only been using Python for a few weeks. (Chose it over Perl,
because Python syntax is cleaner). I really like Python (over C), as it
makes coding and debugging much faster and easier.

Oct 26 '05 #5
the.theorist wrote:
So that it'll be easier to remember the next time I find myself in the
same situation on a different task, I'll extend the discussion
somewhat.

Coming from C, I had expected that I'd get a new empty dict every time
the __init__ function ran. Guido (or some other benevolent) had decided
to implement things a little bit differently in Python. I understand
that most everything is a pointer in Python. (which gives us cool stuff
like recursive data structures) So I was wondering, they could have
made the behavior C-like, but chose not to. The decision to bind
everything in the function to the same default args must be a
reflection of the Python Ideology. Philosophically, why was it done
this way, and how does it fit in with Python's design as a language.
I couldn't claim to be channelling the developers, but I suspect that
the decision was pragmatic.

Consider what would have to be done in the case of a method prototype
such as

class myClass(object):
def __init__(arg1, arg2=default):
print "Arg2 is:", arg2

Remember as well that the "def" statement is executable: when executed
it binds the function name to the compiled function body in the current
namespace.

Suppose the "default" name had gone out of scope by the time the
instances are created: what value would you have the ionterpreter bind
to the argument in that case?
(hopefully, reasons will help me remeber why things are the way they
are, so I don't forget in the future)
-----------------
I've only been using Python for a few weeks. (Chose it over Perl,
because Python syntax is cleaner). I really like Python (over C), as it
makes coding and debugging much faster and easier.

Yup, it's a great languae, and it seems to be increasing in popularity
quite rapidly.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Oct 26 '05 #6


the.theorist wrote:
So that it'll be easier to remember the next time I find myself in the
same situation on a different task, I'll extend the discussion
somewhat.

Coming from C, I had expected that I'd get a new empty dict every time
the __init__ function ran. Guido (or some other benevolent) had decided
to implement things a little bit differently in Python. I understand
that most everything is a pointer in Python. (which gives us cool stuff
like recursive data structures) So I was wondering, they could have
made the behavior C-like, but chose not to. The decision to bind
everything in the function to the same default args must be a
reflection of the Python Ideology. Philosophically, why was it done
this way, and how does it fit in with Python's design as a language.
I didn't read too closely the thread leading up to this point so I may
be stating something already stated or that is already obvious to you.

There is a difference between a pointer and name binding that you should
keep in mind. When using pointers in C you can feasibly have a chain of
pointers. In Python, it doesn't work that way unless you explicitly
define a pointer type object to be used in such a way. The normal
behavior is for an expression to bind a name with the object that is
bound to the other name. So...

a = 1 # a binds to 1
b = a # b binds to 1
c = b # c binds to 1

So if you change what 'a' is bound to, 'b' and 'c' are still each bound
to 1.

Some of the places where you get pointer type behavior is in container
objects. Look at the following sequence of instructions.

a = 1 # a binds to 1
b = [a] # first item in list is bound to 1,
b binds to list.
c = b # c bind to same list b is bound to.
a = 2 # a binds to 2
print b[0] -> 1 # print first item in list b is bound to.
b[0] = 3 # change first item in list b is bound to to 3
print c[0] -> 3 # print first item in list c is bound to,
# (same list as b is bound to)

If you can follow this easily you are well on your way to understanding
Pythons storage model and philosophy. Notice, a name isn't bound to
another name.

This relationship is all over Python including the name binding of
functions and class methods. And also in class's as well.

Keep in mind the class body is executed at the time it is defined, and
the methods are defined but not executed until they are used. In the
case of the __init__ it is when the class is called after an instance
object is created. When instances are created they share any class
objects that were bound to names when the class was defined. They don't
share any objects that get bound to instance attribute names in methods
later.

Does this help any?

Cheers,
Ron
(hopefully, reasons will help me remeber why things are the way they
are, so I don't forget in the future)
-----------------
I've only been using Python for a few weeks. (Chose it over Perl,
because Python syntax is cleaner). I really like Python (over C), as it
makes coding and debugging much faster and easier.

Oct 26 '05 #7

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

Similar topics

1
by: svilen | last post by:
hi. this was named but it is misleading. i want to have the order of setting items in intrinsic dicts (keyword args, or class attributes, etc). which is, a prdered dict trhat matches the...
4
by: G. Richard Bellamy | last post by:
I'm trying to unset the Encrypted attribute on all the files in a path. The attribute is not getting set. What am I doing wrong? Perhaps there's another newsgroup I can send this to? Here's...
3
by: AndyG | last post by:
I'm trying to mess about with a table using javascript. I'm deleting the current row then adding a new row and adding a couple of columns. I then want to add either a style attribute and add a...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
1
by: Steven Quail | last post by:
Hi to all, I have just started looking at XML.Serialization classes and would like to know how I set an attribute for an XML element. For example, I want my XML doc to look like the following:...
7
by: John R. | last post by:
How do you set the following file attributes: Compressed Encrypted Normal ReparsePoint SparsePoint You CAN'T set these using FileInfo.Attributes or File.SetAttributes. It doesn't work for...
3
by: Marty McFly | last post by:
Hello, I have a control class that inherits from System.Web.UI.WebControls.Button. When I drag this control from the "My User Controls" tab in the toolbox onto the form, I want it to reflect the...
0
by: David J | last post by:
Hi, I am strugling with the propertygrid and a listbox. I am using the universaldropdowneditor from the codeproject (code below). However I am populating the listbox via a datasource. The problem...
2
by: Sam | last post by:
Hi all I'm looking for a way to detect the background color of my internal webpage through a stylesheet. And so far, the only solution I find is to open the styles.css file and parse the...
3
by: rh0dium | last post by:
Hi all, I have the following piece of code and I wanted to set the default attributes based on a dictionary. What I am looking for is a way to take PIPODEFAULTS and assign each one as an...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.