|
- Why should we use - gt; in def __init__ (self, n) - gt; None:?
In python 3 5 appeared type annotation option def __init__(self, n) -> None: means that __init__ should always return NoneType and it can be quite helpful if you accidentally return something different from None especially if you use mypy or other similar things
- oop - What do __init__ and self do in Python? - Stack Overflow
In this code: class A(object): def __init__(self): self x = 'Hello' def method_a(self, foo): print self x + ' ' + foo the self variable represents the instance of the object itself Most object-oriented languages pass this as a hidden parameter to the methods defined on an object; Python does not You have to declare it explicitly When you create an instance of the A class and call its
- TypeError: type object is not subscriptable in a function signature
The following answer only applies to Python < 3 9 The expression list[int] is attempting to subscript the object list, which is a class Class objects are of the type of their metaclass, which is type in this case Since type does not define a __getitem__ method, you can't do list[ ] To do this correctly, you need to import typing List and use that instead of the built-in list in your type
|
|
|