r/Python • • Jul 22 '21

Resource Do not use mutable objects as default arguments in Python

https://medium.com/geekculture/do-not-use-objects-as-default-arguments-in-python-1c940212db2e
1.3k Upvotes

218 comments sorted by

View all comments

Show parent comments

21

u/koogas Jul 22 '21

Well, yeah, but this is obvious no?

It's literally the purpose of class attributes

6

u/Efficient-Chair6250 Jul 22 '21

Type hints blurred the lines. It's a trap especially for dataclasses

-3

u/jaredjeya Jul 22 '21

I’d really expect stuff I defined there to be uniquely defined per instance.

12

u/[deleted] Jul 22 '21

Then you should define them as instance properties instead of class properties. The behavior you're describing is exactly their purpose. It's not surprising or a gotcha

0

u/thedominux Jul 22 '21

Explicitly binding objects to instance attributes in __init__ method in my vision is a way just to bind passing parameters to initiate the instance, and binding there default objects, those are the same for every instance of the class so it should belong to the class itself and it's logical IMHO, so it's the reason why I follow this way.

1

u/jaredjeya Jul 22 '21

Wait what’s the difference?

Normally what I do is put a list of variables with their type hints (like x: int) at the start of the class and then give them values in __init__() - is that wrong? Genuinely didn’t know until now that there is a difference there.

Tbh I’ve probably mostly stuck to this, I’ve only generally given the constants actual values in the class rather than __init__(), but I didn’t realise they were defined on a class level rather than an instance level. Is it safe to put just type hints? Or should I have nothing there at all?

3

u/thedominux Jul 22 '21 edited Jul 23 '21

Man, stop thinking about classes as just instance templates! They've got their own lives too)

Moreover you can define a lot of interesting behavior with classes by starting with classmethods and ending with such patterns like the Singleton where you control instance initialization on the class level

3

u/danted002 Jul 23 '21

They can’t they learn OOP thought Java and no one tells them that classes are objects themselves 🙃

3

u/jaredjeya Jul 23 '21

this is literally me lol. I learnt Java a long long time ago and it’s probably just stuck around in my head to influence how I think of everything.

2

u/[deleted] Jul 24 '21

Here are some examples:

from dataclasses import dataclass


class A:
    def __init__(self, a: int):
        # This is an instance property
        self.a = a


>>> a = A(1)
>>> b = A(2)
>>> print(a.a, b.a)
1 2
>>> a.a = 3
>>> print(a.a, b.a)
3 2
>>> 
>>> 
class B:
    # This is a class property
    a = 1

>>> a = B()
>>> b = B()
>>> print(a.a, b.a)
1 1
>>> B.a = 2
>>> print(a.a, b.a)
2 2
>>> a.a = 3
>>> print(a.a, b.a)
3 2


class C:
    # These are just type hints that don't have any functional effect
    a: int
    # This is a misleading type hint. `b` doesn't exist
    b: int
    
    def __init__(self, a):
        self.a = a


>>> a = C(1)
>>> b = C(2)
>>> print(a.a)
1
>>> print(a.b)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    print(a.b)
AttributeError: 'C' object has no attribute 'b'
>>> c = C(2, 3)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    c = C(2, 3)
TypeError: __init__() takes 2 positional arguments but 3 were given
>>> 
>>> 
@dataclass
class D:
    # On a dataclass, these type hints are used to automatically generate a constructor for you like A and C
    a: int
    b: int


>>> a = D(1, 2)
>>> print(a.a, a.b)
1 2

1

u/jaredjeya Jul 25 '21

Thank you so much for sharing this! Good to know type hints don’t cause issues. And I didn’t know about dataclasses!

1

u/[deleted] Jul 25 '21

you're welcome. but i would say that type hints DO cause issues if they're used incorrectly. in the case of dataclasses, they will cause runtime errors if you define the wrong number of fields (though the type doesn't have to be correct), and they can cause development issues if they aren't accurate