Python Classes
Define a Class:
- A class is a custom data type, you specify which variables (called attributes) it will store and which custom functions (called methods) it can call
- When you define a function in a class, you have to pass 'self' as the first parameter
- After defining a class, you can create instances of this type (called Objects, see below)
Create and Use an Object of the class type:
- You can access the variables and functions of the object by naming the object, usuing a dot, and then naming the variable/function
- Although 'self' was listed as a parameter in the function definition, no value should be passed there when called
- This will display 11
Constructors:
- The constructor is a function that is called when you create an object of that type
- It doesn't have to have parameters (apart from 'self'), there is sometimes code that must be performed at initialization
- You can have multiple constructors, each taking a different number of parameters, so you can initiate the object differently accordingly
- The constructor function must be named __init__
- If a class variable is declared in the constructor, it doesn't have to first be declared above it
Special Functions (Magic Methods):
- Special functions specify what to do if certain common python functions or operators are used with an object of this type
- This example defines the addition operator for your class type, so that you can do something like myObj1 + myObj2
- There are many other special functions, not only for mathermatical operations, but for other functions that are called on objects
- Another example is _str__, which allows an object (e.g. myObj) to be converted to a string format using the str command e.g. str(myObj)
- Some other examples are __len__ (get length), __eq__ (check if equal), __getitem__ (for accessing elements of a list with [], by index or slicing)
Private variables (naming convention):
- In some other programming languages, a private variable is one that can only be accessed by functions belonging to the same class
- They are useful if you want to ensure that they are not accessed or modified incorrectly
- Python doesn't enforce private variables, but you can name them this way to let yourself and coders know that it shouldn't be accessed
Getters and Setters:
- Given object "myOb", the above code allows you to access the variable with myOb.myVar without the underscore, while also ensuring that it is set properly
- Notice that it is accessed like a variable, rather than like a function (which would use parentheses)
- The getter allows you to get the value of the private variable, e.g. print( myOb.myVar )
- The setter allows you to set the private variable, e.g. myOb.myVar = 5
Importing a class from a module:
- Import lets your program access code available in another file on your computer
- A module is a .py file with code in it (variables, functions, classes)
- Modules can be made by you, installed, or they may come built in to python
- datetime is a 'module', and it contains the 'date' class
- Alternatively, you could import the whole module with 'import datetime' and access the class as 'datetime.date'
- A package is a folder containing multiple modules, simetimes you have to specify which package a module is in, e.g. from sklearn.linear_model import LogisticRegression
Challenge
Create a class called "Dog" that stores its name, weight, breed, and whether or not it is male; create getters and setters for its variables (make sure weight is greater than 0), and create a single function that will display all of the details. Create a constructor that takes parameters for each of the variables, and a constructor that takes none. If a getter is called for data that is not filled, return a default value of 0, False, or "none".
In the main class, create two instances/objects of this class (one with the parameters and one without). For the object created without parameters, call a getter to ensure that the default value is returned, then set the values with the setters, and print the values accessed with the getters. Finally, run the display function for the object created with parameters.