Unsure of the Python convention for type hinting instance variables - I\'ve been doing them within the __init__
constructor arguments like seen here:
I'd stick with what you're doing in LoggedVar, it follows the same rules as everywhere else in Python so less confusion all round.
The BasicStarShip class changes the scope of the variables by moving them out of the __init__ function. With captain declared there BasicStarShip.captain will return 'Picard'.
The PEP526 annotations are nice to read, but it's a new rule for one specific case, the __init__ function. From the Zen of Python: "Special cases aren't special enough to break the rules."
I would recommend using the first version, where you assign types to your __init__
method's parameters, for most circumstances.
That particular method has the least amount of redundancy while still allowing type checkers to verify that you're calling that __init__
method correctly elsewhere in your code.
I would recommend using either the second or third version, where you explicitly annotate your fields (inside or outside __init__
) when your __init__
method has grown complex enough to the point where one or more of the following apply:
However, it was unclear to me whether the second or third version was preferred -- I personally prefer the third version because it's more conceptually cleaner and doesn't seem to mix the notion of instance vs class attributes, but I can't deny the second version looks cleaner.
I asked about it on the 'typing' gitter channel, and got the following response from Guido (who, on the off-chance you didn't know, made Python and is currently working on mypy and typing related stuff):
There seem to be strong opinions either way. I do indeed prefer putting attribute annotations in the class body rather than sprinkling them throughout
__init__
and other methods. I also think that with PEP 526 this will be the future (also with things like class-based NamedTuple declarations and possibly https://github.com/ericvsmith/dataclasses).
(link to quote)
So, it seems like the second version is recommended over the third, and that defining classes in that manner will become more deeply integrated into the Python language itself at some point in the future!
Edit: PEP 557, data classes was recently accepted and appears to be on-track (?) to be included with Python 3.7.