I am new to Python coming from PHP, I know Python has Docstrings, but how can I do this on class variables? Let's say in PHP I have:
class Animal {
/**
* @var Noise
*/
public $noise
}
Now how would I implement this is python? Something like:
class Animal:
# Here I want to tell my IDE that this is a noise object, not yet set
self.noise = None
These are called "attribute docstrings." You can put the docstring after the declaration of the class, or in the __init__
method:
From PEP 257:
String literals occurring immediately after a simple assignment at the top level of a module, class, or init method are called "attribute docstrings".
(see also PEP 258).
There are lots of resources on this, just search for "attribute docstrings," e.g., this answer.