Python Classes and Objects
- Previous Page Python Arrays
- Next Page Python Inheritance
Python Class/Object
Python is an object-oriented programming language.
Almost everything in Python is an object, with properties and methods.
A class (Class) is similar to an object constructor, or a 'blueprint' for creating objects.
Create a class
To create a class, use class
Keywords:
Instance
Create a class named MyClass using the property named x:
class MyClass: x = 5
Create an object
Now we can use the class named myClass to create objects:
Instance
Create an object named p1 and print the value of x:
p1 = MyClass() print(p1.x)
__init__() function
The example above is the simplest form of class and object, which is not truly useful in practical applications.
To understand the meaning of classes, we must first understand the built-in __init__()
Function.
All classes have a function named __init__(), which is always executed when the class is started.
Assign values to object properties using the __init__() function, or perform other operations that are required when creating an object:
Instance
Create a class named Person and use the __init__() function to assign values to name and age:
class Person: def __init__(self, name, age): self.name = name self.age = age p1 = Person("Bill", 63) print(p1.name) print(p1.age)
Note:Every time a new object is created using the class, it willAutomatically called __init__() function.
Object methods
Objects can also contain methods. Methods in an object are functions that belong to the object.
Let's create a method in the Person class:
Instance
Insert a function to print a greeting and execute it on the p1 object:
class Person: def __init__(self, name, age): self.name = name self.age = age def myfunc(self): print("Hello my name is " + self.name) p1 = Person("Bill", 63) p1.myfunc()
Tip:The 'self' parameter is a reference to the current instance of the class, used to access variables that belong to the class.
self Parameter
self
The parameter is a reference to the current instance of the class, used to access variables belonging to the class.
It does not have to be named self
, you can call it freely, but it must be a function of any function in the classFirst Parameter:
Instance
Use the word mysillyobject and abc instead of self:
class Person: def __init__(mysillyobject, name, age): mysillyobject.name = name mysillyobject.age = age def myfunc(abc): print("Hello my name is " + abc.name) p1 = Person("Bill", 63) p1.myfunc()
Modify Object Attribute
You can modify the object's attributes in this way:
Instance
Set p1's age to 40:
p1.age = 40
Delete Object Attribute
You can use del
Keyword to delete an object's attribute:
Instance
Delete the age attribute of p1 object:
del p1.age
pass Statement
Class definitions cannot be empty, but if for some reason you wrote a class definition statement without content, please use the pass statement to avoid errors.
Instance
class Person: pass
- Previous Page Python Arrays
- Next Page Python Inheritance