Anyways my best interpretation of a class is: "Classes are constructs that have a set of defined methods, such that any objects instantiated from this class will have access to those methods."
The methods that classes have are just functions that can be accessed with class_name.method_name(foo). In addition, there are special methods which Python can call for special operators. Special methods are denoted with "__" in front and in the back of the method name. Classes should at minimum have __init__, __eq__, and __str__ special methods.
Special methods:
__init__: Instructions on how to instantiate an object of the class
__eq__: Compares two objects and determines if they are the same
__str__: The str representation of an object of the class
There are also public and private methods. Private methods are denoted with "_" in front of the method name. There is actually no protection from someone calling them but serves to tell a reader that it should not be called.
Classes are useful since they provide a common API for various things that may need it. This saves a lot of rewriting code when they are inherited. This is called inheritance and will probably be talked about in a later SLOG.
No comments:
Post a Comment