Python Classroom notes 19/Dec/2024

Python – Object Oriented Programming

  • Object oriented programming simplifies creation of systems
  • An object can be anything (bank account, transaction, student, course …)
  • Every object has
    • contents: What it has
    • capabilities: What it does
  • To design a system, we identify objects and design classes
  • Class is an design time entity where as object is real world entitity
  • To create an object we need a class, this process of creating object from classes is called as instantiation.
  • Examples of Class and object
Class Object
Car swift car with black color
Bank Account account with number 10001 and balance of 10000
  • Contents for attributes or memebers of the clases and Capabilites form methods (function)
  • Class can be related to each other
    • inheritence: is-a
    • composition: has-a
  • Class Diagram Arrows

Case Study

  • Design classes for vehicle parking system in a mall
  • Come up with classes (attributes and methods)
  • Relationship between classes

Class DocString

To document class attributes in a Google style docstring, you should include an Attributes section within the class docstring. This section should list each attribute along with its type and a brief description. Here’s how to structure it:

Structure for Documenting Class Attributes

  1. Class Docstring: Begin with a summary line followed by a more detailed description, if necessary.
  2. Attributes Section: Use the heading “Attributes:” to introduce the list of attributes.
  3. Attribute Format: Each attribute should be documented in the following format:
  4. attribute_name (type): Description of the attribute.

Example

Here’s a sample class demonstrating this format:

class ExampleClass:
    """A class representing an example entity.

    This class demonstrates how to structure a class docstring
    according to the Google Python Style Guide.

    Attributes:
        attr1 (str): Description of `attr1`.
        attr2 (int, optional): Description of `attr2`. Defaults to 0.
    """

    def __init__(self, param1, param2=0):
        """Initializes ExampleClass with the provided parameters.

        Args:
            param1 (str): Description of `param1`.
            param2 (int, optional): Description of `param2`. Defaults to 0.
        """
        self.attr1 = param1
        self.attr2 = param2

Key Points

  • Public Attributes: Document public attributes in the Attributes section.
  • Private Attributes: Typically, private attributes are not documented unless necessary.
  • Properties: If using properties, document them in their getter methods rather than in the class docstring.

This structure ensures clarity and consistency in documenting class attributes, making your code easier to understand and maintain[1][4][7].

Citations:
[1] https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
[2] https://gist.github.com/redlotus/3bc387c2591e3e908c9b63b97b11d24e
[3] https://www.reddit.com/r/learnpython/comments/12rvcg1/confused_by_googles_docstring_attributes_section/
[4] https://stackoverflow.com/questions/63018394/python-docstring-google-style-how-to-document-class-attributes
[5] https://www.datacamp.com/tutorial/docstrings-python
[6] https://github.com/microsoft/pylance-release/issues/3347
[7] https://gemseo.readthedocs.io/en/5.3.2/software/example_google_docstring.html
[8] https://discuss.python.org/t/revisiting-attribute-docstrings/36413?page=2

Published
Categorized as Uncategorized Tagged

By continuous learner

devops & cloud enthusiastic learner

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Media Icons Powered by Acurax Web Design Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%