Send Close Add comments: (status displays here)
Got it!  This site "www.robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python classes
by RS  admin@robinsnyder.com : 1024 x 640


1. Python classes
An object is an instance of a class.


2. Minimal class
Here is a minimal Python class. The __name__ attribute of the class is the name of that class.

Here is the Python code [#1]


3. Docstrings
In Python, a docstring is a string literal that is the first element of a module, function, class or method definition.

That string literal becomes the __doc__ attribute of that module, function, class or method.

4. Code example
Here is a better minimal class that uses a docstring. A common convention is to use triple double quotes to delimit the docstring.

Here is the Python code [#2]

Here is the output of the Python code.


5. Python str function
The Python str function returns a string representation of the supplied actual parameter.

6. Class example
Here is an example of the str function with a minimal class.

Here is the Python code [#3]

Here is the output of the Python code.

This is not very meaningful.

7. Class str example
Here is the Python code [#4]

Here is the output of the Python code.

This can be more meaningful.

8. Class dictionaries
Many aspects of a Python class are accessible from the class dictionary or other methods. Here is the Python code [#5]

Here is the output of the Python code.


9. Reflection
Code reflection is the ability of code in a language to accesses parts of it's own code.

The above shows how Python makes it easy to do code reflection.


10. Self
Notice the use of self in a class. The passed variable self allows the object instance to access the object variables. The name does not need to be self but that is the convention.


11. Python str and repr
The Python str function is used to get the informal output intended for display to the user.

The Python repr function is used to get the formal output intended for use in debugging and development.


12. Modules
A object of a class acts as a cloned module.

This is especially obvious in Python when one looks at the module and class structure. The module code is put inside a class definition, a constructor mechanism is added, and a name usually called self is added to allow that instance to access object members.


13. End of page

by RS  admin@robinsnyder.com : 1024 x 640