Eric V. Smith <e...@trueblade.com> added the comment:

The problem is that __init__ has to have a sentinel to know whether or not to 
call the default value. If the default were just "dict", should it call it, or 
is the passed in value really dict, in which case it doesn't get called?

dataclass()
class Test:
    a: int
    b: Dict[Any, Any] = field(default_factory=dict)

The generated __init__ looks like:

def __init__(self, a, b=_HAS_DEFAULT_FACTORY):
   self.a = a
   self.b = dict() if b is _HAS_DEFAULT_FACTORY else b

If it were:

def __init__(self, a, b=dict):
   self.a = a

Then what would the assignment to self.b look like? What if you instantiated an 
object as Test(0, dict)? You wouldn't want dict to get called. You need to 
differentiate between Test(0, dict) and Test(0). The former does not call b, 
but the latter does call b.

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39078>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to