The operation functions and statement blocks mentioned above are traditional process-oriented programming, while large programs are usually written in object-oriented programming. Classes and objects are two main aspects of object-oriented programming. Classes create a new type, and objects are class instances. Python has no value type or reference type. It treats all things as classes. Class is created using the class keyword.
1. Self
Class methods have only one special difference from common functions-they must have an additional first parameter name, but you do not assign a value to this parameter when calling this method, python provides this value. This special variable refers to the object itself, which is named self by convention.
You must be wondering how Python assigns values to self and why you don't need to assign values to it. This can be clarified by an example. Assume that you have a class named myclass and an instance named myobject of this class. When you call this object method myobject. method (arg1, arg2), this will be automatically converted from Python to myclass. method (myobject, arg1, arg2) -- this is the principle of self.
2. Class
An empty class:
class Person: pass #An empty block
Class usage:
p = Person()print(p)
3. Method
Classes/objects can have methods like functions. The difference between these methods and functions is only an extra self variable.
class Person: def sayHi(self): print("Hello, how are you?")p = Person()p.sayHi()
Note that the sayhi method is called without any parameters, but there is still self in the function definition.
4. _ init _ Method
There are many methods in the python class whose names have special significance. Like _ init __, it is similar to constructor. The _ init _ method runs immediately when an object of the class is created. This method can be used to initialize your object. Note that the start and end of the name are double underscores.
class Person: def __init__(self, name): self.name = name def sayHi(self): print("Hello, my name is", self.name)p = Person("known")p.syaHi()5. Domain
Python has two types of domains --Class variablesAndObject VariablesThey are differentiated based on whether the class or object owns the variable.
Class variablesShared by all objects (instances) of a class. Only one class variable is copied, so when an object changes the class variable, this change will be reflected on all other instances.
Object VariablesIt is owned by each object/instance of the class. Therefore, each object has its own copy of this domain, that is, they are not shared. In different instances of the same class, although the object variables have the same name, however, they are unrelated.
class Person: '''Represnets a person.''' population = 0 def __init__(self, name): '''Initializes the person's data.''' self.name = name print("(Initializing %s)" % self.name) #When this person is created, he/she adds to the population Person.population += 1 def __del__(self): '''I am dying.''' print("%s says bye." % self.name) Person.population -= 1 if Person.population == 0: print("I am the last one.") else: print("There are still %d people left." % Person.population) def sayHi(self): '''Greeting by the person. Really, that's all it does.''' print("Hi, my name is %s." % self.name) def howMany(self): '''Prints the current population.''' if Person.population == 1: print("I am the only person here.") else: print("We have %d person here." % Person.population)swaroop = Person('Swaroop')swaroop.sayHi()swaroop.howMany()kalam = Person('Abdul Kalam')kalam.sayHi()kalam.howMany()swaroop.sayHi()swaroop.howMany()del kalamdel swaroop
Running result:
(Initializing swaroop)
Hi, my name is swaroop.
I am the only person here.
(Initializing Abdul Kalam)
Hi, my name is Abdul Kalam.
We have 2 persons here.
Hi, my name is swaroop.
We have 2 persons here.
Abdul Kalam says bye.
There are still 1 people left.
Swaroop says bye.
I am the last one.
6. Inheritance
The class name is followed by a pair of parentheses, and the base class name is written in parentheses.
class SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.age = age print("(Initialized SchoolMember: %s)" % self.name) def tell(self): '''Tell my details.''' print("Name:'%s' Age:'%s'" % (self.name, self.age))class Teacher(SchoolMember): '''Represents a teacher.''' def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) self.salary = salary print("(Initialized Teacher: %s)" % self.name) def tell(self): SchoolMember.tell(self) print("Salary: '%d'" % self.salary)class Student(SchoolMember): '''Represents a student.''' def __init__(self, name, age, marks): SchoolMember.__init__(self, name, age) self.marks = marks print("(Initialized Student: %s)" % self.name) def tell(self): SchoolMember.tell(self) print("Marks: '%d'" % self.marks)t = Teacher("Mrs. Shrividya", 40, 30000)s = Student("Swaroop", 22, 75)print() # prints a blank linemembers = [t, s]for member in members: member.tell() # works for both Teachers and Students
Output result:
(Initialized schoolmember: Mrs. shrividya)
(Initialized Teacher: Mrs. shrividya)
(Initialized schoolmember: swaroop)
(Initialized Student: swaroop)
Name: 'Mrs. shrividya' age: '40' salary: '000000'
Name: 'swaroop 'age: '22' marks: '75'