Python class constructor with default arguments [duplicate] - python

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
Can anyone explain the following strange behaviour?
I have the following class:
class Zoo:
def __init__(self,alist=[]):
self.animals = alist
def __len__(self):
return len(self.animals)
def add(self,a):
self.animals.append(a)
and when I do the following,
In [38]: z=Zoo()
In [39]: z.add(2)
In [40]: z.add(23)
In [41]: len(z)
Out[41]: 2
In [42]: z2=Zoo()
In [43]: len(z2)
Out[43]: 2
Why is z2.animals not an empty list?
Thanks, Matthias

You are mutating the default argument in your constructor (you are just copying a reference to the same list into each of your instances). You can fix this as follows:
class Zoo:
def __init__(self,alist=None):
self.animals = alist or []
def __len__(self):
return len(self.animals)
def add(self,a):
self.animals.append(a)

The default argument list is the same object for all instances, hence assigning it to a member just assigns a reference to the same object.
here's an example:
>>> class foo():
... def __init__(self, x = []):
... print id(x)
...
>>> x = foo()
140284337344168
>>> y = foo()
140284337344168
>>> z = foo()
140284337344168
you can see that x is the same object in all instances.

Related

Class variable vs instance variable

While learning python through python docs, i came across the following wherein its explained that class variable is common to the class and that any object can change it:
Sample Code 1:
class Dog:
tricks = [] # mistaken use of a class variable
def __init__(self, name):
self.name = name
def add_trick(self, trick):
self.tricks.append(trick)
Output:
>>> d = Dog('Fido')
>>> e = Dog('Buddy')
>>> d.add_trick('roll over')
>>> e.add_trick('play dead')
>>> d.tricks # unexpectedly shared by all dogs
['roll over', 'play dead']
Question => If so, then why doesn't y in the following example get affected when x changes its tricks attribute to 5?
Sample Code 2:
class Complex:
tricks = 3
def __init__(self,var1):
self.tricks=var1
def add_tricks(self,var1):
self.tricks=var1
x = Complex(11)
y = Complex(12)
print (x.tricks)
print (y.tricks)
x.add_tricks(5)
print (x.tricks)
print (y.tricks) -->Remains unchanged
Output:
11
12
5
12 -->Remains unchanged
And what exactly is the difference when i remove the self in the following program:
Sample Code 3:
class Complex:
tricks = 3
def __init__(self,var1):
self.tricks=var1
def add_tricks(self,var1):
tricks=var1
x = Complex(11)
y = Complex(12)
print (x.tricks)
print (y.tricks)
x.add_tricks(5) -->This change is not reflected anywhere
print (x.tricks)
print (y.tricks)
print(Complex.tricks)
Output:
11
12
11
12
3
This example may be illustrative. Given the following class (I've dropped the initialiser from your example because it doesn't let us demonstrate the behaviour):
class Complex:
tricks = 3
def add_tricks(self, value):
self.tricks = value
We can see, upon creation, the value of their tricks attribute is both 3:
>>> a = Complex()
>>> b = Complex()
>>>
>>> a.tricks
3
>>> b.tricks
3
Let's take a second and look at the names defined on those objects:
>>> a.__dict__
{}
>>> b.__dict__
{}
They're both objects with no attributes themselves. Let's see what happens after we call add_tricks on b:
>>> b.add_tricks(5)
>>>
>>> a.tricks
3
>>> b.tricks
5
Okay. So, this looks like the shared value hasn't been affected. Let's take a look at their names again:
>>> a.__dict__
{}
>>> b.__dict__
{'tricks': 5}
And there it is. Assigning to self.tricks creates an attribute local to that object with name tricks, which when accessed via the object (or self) is the one that we'll use from that point forward.
The shared value is still there and unchanged:
>>> a.__class__.tricks
3
>>> b.__class__.tricks
3
It's just on the class, not on the object.

Python creating new object of class contains values of older object [duplicate]

This question already has answers here:
"Least Astonishment" and the Mutable Default Argument
(33 answers)
Closed 7 years ago.
I have got a class with a list-variable and a function to append items to this list. I cannot append items directly, because I have to validate them:
class Foo:
def __init__(self, elements=[]):
self.elements = elements
def append(self, element):
self.elements.append(element)
If I instantiate an object of this class, add items and then create another new object, this object contains the items of the first object.
foo = Foo()
print foo.elements # []
foo.append(element=4)
print foo.elements # [4]
foo.append(element=7)
print foo.elements # [4, 7]
bar = Foo()
print bar.elements # [4, 7]
Can someone explain my, why this happens?
A possible solution for me could be this, but I don't like it...
class Foo:
def __init__(self, elements=None):
if elements is None:
self.elements = []
else:
self.elements = elements
def append(self, element):
self.elements.append(element)
Thanks for all answers!
You've got your answer yourself already. Initializing with a mutable object ist never a good idea. It will be initialized once the class gets defined and reused for every instance.

In python __init__ dictionary arguments defaulting to previous class values [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
“Least Astonishment” in Python: The Mutable Default Argument
I'm finding that dictionary arguments to the init() function of my class are defaulting to values I've previously set in previous instances. I really don't understand this behavior, and it doesn't seem to happen with lists or basic variables. Example code:
class TestClass:
def __init__(
self,
adir={},
alist=[],
avar=None
):
print("input adir: " + str(adir)) #for test2, shows test1.mydir
self.mydir = adir
self.mylist = alist
self.myvar = avar
test1 = TestClass()
test1.mydir['a'] = 'A'
test1.mylist = ['foo']
test1.myvar = 5
test2 = TestClass()
print(test2.mydir) #has same value of test1!
print(test2.mylist)
print(test2.myvar)
The output looks like this:
initializing test1
input adir: {}
initializing test2
input adir: {'a': 'A'}
{'a': 'A'}
[]
None
Why does the dictionary argument (adir) to test2 get set to test1.mydir? Especially, why is the behaviour different than other mutable types like list?
Thank you!
As DSM says, don't modify mutable default arguments. Do this instead:
class TestClass:
def __init__(self, adir=None, alist=None, avar=None):
if alist is None:
alist = []
if adir is None:
adir = {}

Static member of a function in Python ? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Static class variables in Python
What is the Python equivalent of static variables inside a function?
How can I use static fields in Python ?
for example i want to count how many times the function has been called - how can i do this ?
If you wish to count how many times a method has been called, no matter which instance called it, you could use a class member like this:
class Foo(object):
calls=0 # <--- call is a class member
def baz(self):
Foo.calls+=1
foo=Foo()
bar=Foo()
for i in range(100):
foo.baz()
bar.baz()
print('Foo.baz was called {n} times'.format(n=foo.calls))
# Foo.baz was called 200 times
When you define calls this way:
class Foo(object):
calls=0
Python places the key-value pair ('calls', 0) in Foo.__dict__.
It can be accessed with Foo.calls.
Instances of Foo, such as foo=Foo(), can access it with foo.calls as well.
To assign new values to Foo.calls you must use Foo.calls = ....
Instances can not use foo.calls = ... because that causes Python to place a new and different key-value pair in foo.__dict__, where instance members are kept.
Here's a decorator adding counting to a function.
import functools
def count_calls(func):
#functools.wraps(func)
def decor(*args, **kwargs):
decor.count += 1
return func(*args, **kwargs)
decor.count = 0
return decor
Usage:
>>> #count_calls
... def foo():
... pass
...
>>> foo.count
0
>>> foo()
>>> foo.count
1
Here is some example counting the number of calls of all objects of the same class:
class Swallow():
i = 0 # will be used for counting calls of fly()
def fly(self):
Swallow.i += 1
And this is the proof:
>>> a = Swallow()
>>> b = Swallow()
>>> a.fly()
>>> a.i
1
>>> Swallow.i
1
>>> b.fly()
>>> b.i
2
>>> Swallow.i
2
so you can read it by giving the object name or class name.
Here's one simplistic way to do it:
def func():
if not hasattr(func, 'counter'):
func.counter = 0
func.counter += 1
counter = 0 # Not the same as `func.counter`
print(func.counter)
Or if you don't like the if being executed on every call, you can do:
def func():
func.counter += 1
print(func.counter)
func.counter = 0

How should I declare default values for instance variables in Python?

Should I give my class members default values like this:
class Foo:
num = 1
or like this?
class Foo:
def __init__(self):
self.num = 1
In this question I discovered that in both cases,
bar = Foo()
bar.num += 1
is a well-defined operation.
I understand that the first method will give me a class variable while the second one will not. However, if I do not require a class variable, but only need to set a default value for my instance variables, are both methods equally good? Or one of them more 'pythonic' than the other?
One thing I've noticed is that in the Django tutorial, they use the second method to declare Models. Personally I think the second method is more elegant, but I'd like to know what the 'standard' way is.
Extending bp's answer, I wanted to show you what he meant by immutable types.
First, this is okay:
>>> class TestB():
... def __init__(self, attr=1):
... self.attr = attr
...
>>> a = TestB()
>>> b = TestB()
>>> a.attr = 2
>>> a.attr
2
>>> b.attr
1
However, this only works for immutable (unchangable) types. If the default value was mutable (meaning it can be replaced), this would happen instead:
>>> class Test():
... def __init__(self, attr=[]):
... self.attr = attr
...
>>> a = Test()
>>> b = Test()
>>> a.attr.append(1)
>>> a.attr
[1]
>>> b.attr
[1]
>>>
Note that both a and b have a shared attribute. This is often unwanted.
This is the Pythonic way of defining default values for instance variables, when the type is mutable:
>>> class TestC():
... def __init__(self, attr=None):
... if attr is None:
... attr = []
... self.attr = attr
...
>>> a = TestC()
>>> b = TestC()
>>> a.attr.append(1)
>>> a.attr
[1]
>>> b.attr
[]
The reason my first snippet of code works is because, with immutable types, Python creates a new instance of it whenever you want one. If you needed to add 1 to 1, Python makes a new 2 for you, because the old 1 cannot be changed. The reason is mostly for hashing, I believe.
The two snippets do different things, so it's not a matter of taste but a matter of what's the right behaviour in your context. Python documentation explains the difference, but here are some examples:
Exhibit A
class Foo:
def __init__(self):
self.num = 1
This binds num to the Foo instances. Change to this field is not propagated to other instances.
Thus:
>>> foo1 = Foo()
>>> foo2 = Foo()
>>> foo1.num = 2
>>> foo2.num
1
Exhibit B
class Bar:
num = 1
This binds num to the Bar class. Changes are propagated!
>>> bar1 = Bar()
>>> bar2 = Bar()
>>> bar1.num = 2 #this creates an INSTANCE variable that HIDES the propagation
>>> bar2.num
1
>>> Bar.num = 3
>>> bar2.num
3
>>> bar1.num
2
>>> bar1.__class__.num
3
Actual answer
If I do not require a class variable, but only need to set a default value for my instance variables, are both methods equally good? Or one of them more 'pythonic' than the other?
The code in exhibit B is plain wrong for this: why would you want to bind a class attribute (default value on instance creation) to the single instance?
The code in exhibit A is okay.
If you want to give defaults for instance variables in your constructor I would however do this:
class Foo:
def __init__(self, num = None):
self.num = num if num is not None else 1
...or even:
class Foo:
DEFAULT_NUM = 1
def __init__(self, num = None):
self.num = num if num is not None else DEFAULT_NUM
...or even: (preferrable, but if and only if you are dealing with immutable types!)
class Foo:
def __init__(self, num = 1):
self.num = num
This way you can do:
foo1 = Foo(4)
foo2 = Foo() #use default
Using class members to give default values works very well just so long as you are careful only to do it with immutable values. If you try to do it with a list or a dict that would be pretty deadly. It also works where the instance attribute is a reference to a class just so long as the default value is None.
I've seen this technique used very successfully in repoze which is a framework that runs on top of Zope. The advantage here is not just that when your class is persisted to the database only the non-default attributes need to be saved, but also when you need to add a new field into the schema all the existing objects see the new field with its default value without any need to actually change the stored data.
I find it also works well in more general coding, but it's a style thing. Use whatever you are happiest with.
With dataclasses, a feature added in Python 3.7, there is now yet another (quite convenient) way to achieve setting default values on class instances. The decorator dataclass will automatically generate a few methods on your class, such as the constructor. As the documentation linked above notes, "[t]he member variables to use in these generated methods are defined using PEP 526 type annotations".
Considering OP's example, we could implement it like this:
from dataclasses import dataclass
#dataclass
class Foo:
num: int = 0
When constructing an object of this class's type we could optionally overwrite the value.
print('Default val: {}'.format(Foo()))
# Default val: Foo(num=0)
print('Custom val: {}'.format(Foo(num=5)))
# Custom val: Foo(num=5)
Using class members for default values of instance variables is not a good idea, and it's the first time I've seen this idea mentioned at all. It works in your example, but it may fail in a lot of cases. E.g., if the value is mutable, mutating it on an unmodified instance will alter the default:
>>> class c:
... l = []
...
>>> x = c()
>>> y = c()
>>> x.l
[]
>>> y.l
[]
>>> x.l.append(10)
>>> y.l
[10]
>>> c.l
[10]
You can also declare class variables as None which will prevent propagation. This is useful when you need a well defined class and want to prevent AttributeErrors.
For example:
>>> class TestClass(object):
... t = None
...
>>> test = TestClass()
>>> test.t
>>> test2 = TestClass()
>>> test.t = 'test'
>>> test.t
'test'
>>> test2.t
>>>
Also if you need defaults:
>>> class TestClassDefaults(object):
... t = None
... def __init__(self, t=None):
... self.t = t
...
>>> test = TestClassDefaults()
>>> test.t
>>> test2 = TestClassDefaults([])
>>> test2.t
[]
>>> test.t
>>>
Of course still follow the info in the other answers about using mutable vs immutable types as the default in __init__.

Categories

Resources