typing instance variable
David KolovratnÃk
david at kolovratnik.net
Fri May 14 02:28:48 EDT 2021
Greetings,
This snippet of code raises ValueError during execution:
class MyClass:
__slots__ = 'foo'
foo: int = 0
mypy does not complain:
mypy myclass.py
Success: no issues found in 1 source file
But python3 does:
python3 myclass.py
Traceback (most recent call last):
File "myclass.py", line 1, in <module>
class MyClass:
ValueError: 'foo' in __slots__ conflicts with class variable
This is where the way of type declaration of instance variable comes from:
https://mypy.readthedocs.io/en/stable/cheat_sheet_py3.html
class MyClass:
# You can optionally declare instance variables in the class body
attr: int
# This is an instance variable with a default value
charge_percent: int = 100
Why Python believes foo is class variable?
python3 -V
Python 3.6.8
Kind regards,
David
More information about the Python-list
mailing list