0

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
superdee
  • 637
  • 10
  • 23
  • https://stackoverflow.com/questions/3898572/what-is-the-standard-python-docstring-format I've been using three single quotes, my info, and three single quotes. – sniperd Jul 31 '18 at 18:24
  • In this case, should I right a function like get_noise() that returns self.noise, and add the Docstring there? – superdee Jul 31 '18 at 18:28

1 Answers1

2

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.

sundance
  • 2,905
  • 4
  • 21
  • 31