Python Classes
Python classes are a way to group data under a certain banner. You've been using Python classes, but might not have realized it. For example, file objects (created using the open() function) are a class.
Syntax
Classes can be created by using the class keyword, followed by a unique name for your class:
1 class COSC505:
2 def __init__(self, value_of_variable=100):
3 self.some_variable = value_of_variable
4
5 def function_in_class(self):
6 print(self.some_variable)
7
8 cosc_505 = COSC505(7000)
9 cosc_505.function_in_class()
The def __init__ is a special function that Python calls when the object is created (on line 8 in the code above). Notice that self is automatically populated with the object in memory. That is why when I specify COSC505(7000), the integer value 7000 goes into the value_of_variable instead of self.
Most special functions in Python have the same look (two underscores, a name, and two more underscores): __eq__, __str__.
For example, the following statements are equivalent:
if myclass == yourclass:
print("They're equal")
if myclass.__eq__(yourclass):
print("They're equal")
The __eq__ function is called whenever the equals-to operator == is used. Others would be something like: __lt__ for < and so forth.
Example
The functions inside of a class are typically used to modify the internal data. Python does not allow fine-grained access protection like other programming languages.
Student
class Student:
def __init__(self, name, id):
self.set_name(name)
self.set_id(id)
def set_name(self, name):
self.name = name
def set_id(self, id):
if id > 0 and type(id) == int:
self.id = id
else:
print("ERROR: id must be a positive integer")
def get_name(self):
return self.name
def get_id(self):
return self.id
In the code above, the __init__ function makes sure that all of the internal variables are set properly. Notice that if I didn't have an __init__ function, the get_name() and get_id() would try to access self.name and self.id, which wouldn't exist. Just like any other variable, I cannot do the following:
print(a)
In the code above, the variable a does not exist when I call print. Instead, I need to make sure the variable is assigned, then I can print it:
a = 100
print(a)
Data Type
A class creates a new data type, so classes can be added to lists just like integers and other data types:
a = Student("John", 100)
b = Student("Jane", 200)
students = [a]
students.append(b)
print(students)
The code above creates two Student objects (objects are classes that have space in memory) and then adds both to the list. a is directly added to the list and b is appended to the list.
The code above will print something similar to the following:
[<__main__.Student object at 0x7fd36f2c75c0>, <__main__.Student object at 0x7fd36f2c7630>]
The 0x7fd36f2c75c0 is the memory address that this student object occupies.
Type type of the class in Python is the name of the class:
a = Student("John", 100)
if type(a) == Student:
print("a is a Student type")
else:
print("a is NOT a Student type")
The code above will print a is a Student type, since was created as a Student object.
Stephen Marz (20190605)