Notes:
- The __init__ method is a special method that will be called automatically when an instance of the class is created.
- The self parameter is a reference to the instance.
- The self parameter is added automatically when calling a method of the class.
- Variables defined using the self keyword will be accessible to all methods in the class and to all instances of the class.
Defining a Class:
Creating an instance of a class:
Calling methods of an instance:
Accessing attributes of an instance:
Modifying attributes of an instance:
Inheritance:
Creating an instance of the subclass:
Accessing attributes of an instance of a subclass:
Calling methods of an instance of a subclass:
Overriding methods from the parent class:
Calling override methods of an instance of a subclass:
Storing classes in modules:
Create a file
file1.py that contains the definition of two classes:
Importing modules:
Create a file
file2.py that imports the module
file1 (
file1.py):
Importing specific classes defined in a module:
Create a file
file3.py that imports specific classes defined in the module
file1 (
file1.py):
Importing all classes in a module:
Create a file
file4.py that imports all classes defined in the module
file1 (
file1.py):
Using the keyword
as to define an alias for a module:
Create a file
file5.py that imports the module
file1 (
file1.py) and give it an alias
f1:
Using the keyword
as to define an alias for a class:
Create a file
file6.py that imports specific functions defined in the module
file1 (
file1.py) and give them aliases: