Object Orientation in Python

Python is an object-oriented language. Briefly summarize some of the object-oriented features in Python.

1. Create a class:

Write a simple example:

class Student:
   'Base class for all students'
   stuCount = 0//class variable
 
   def __init__(self, name, student_id):
      self.name = name
      self.student_id = student_id
      Student.stuCount += 1
   
   def displayCount(self):
     print "Total Student %d" % Student.stuCount
   def display(self):
      print "Name : ", self.name,  ", Stuendt_id: ", self.student_id

 1) empCount variable is a class variable and its value will be shared among all instances of this class, accessed by Employee.empCount.

2) The first method __init__() method is a special method called the class constructor or initialization method (somewhat different from other object-oriented instantiations), when an instance of this class is created will call this method

3) self represents an instance of a class, self is necessary when defining a method of a class, although it is not necessary to pass in the corresponding parameters when calling.

2. Create an instance object:

similar to function call

"Create the first object of the Student class"
stu1 = Student("zhangsan", 00001)
"Create a second object of the Student class"
stu2 = Student("wnagwu", 00002)

 Use " . " to call properties

You can also directly add, modify, delete attributes

stu1.age = 7 # add an 'age' property
stu1.age = 8 # Modify the 'age' property
del stu1.age # delete the 'age' attribute

 You can also access properties using the following functions:

getattr(obj, name[, default]) : Access the attributes of the object.

hasattr(obj,name) : Check if an attribute exists.

setattr(obj,name,value) : Set an attribute. If the attribute does not exist, a new attribute is created.

delattr(obj, name) : delete the attribute.

3. Inheritance of classes:

Syntax: class Derived class name (base class name) The base class name is written in parentheses, and the base class is specified in the tuple when the class is defined.

Some features of inheritance in python:

1) In inheritance, the construction of the base class (__init__() method) will not be called automatically, it needs to be called specifically in the construction of its derived class.

2) When calling the method of the base class, you need to add the class name prefix of the base class, and you need to bring the self parameter variable. Unlike calling ordinary functions in a class, you do not need to take the self parameter

3) Python always looks for the method of the corresponding type first, and if it cannot find the corresponding method in the derived class, it starts to look up one by one in the base class. (First look for the called method in this class, and then go to the base class if you can't find it).

Multiple inheritance of classes:

grammar:

 

class SubClassName (ParentClass1[, ParentClass2, ...]):
   'Optional class documentation string'
   class_suite

 example:

wrote
class Parent: # Define the parent class
parentAttr = 100
def __init__(self):
print "call the parent class constructor"

def parentMethod(self):
print 'call the parent class method'

def setAttr(self, attr):
Parent.parentAttr = attr

def getAttr(self):
print "Parent class attribute:", Parent.parentAttr

class Child(Parent): # Define subclass
def __init__(self):
print "Call subclass constructor"

def childMethod(self):
print 'Call Child class method child method'

c = Child() # Instantiate child class
c.childMethod() # Call child class method
c.parentMethod() # Call parent class method
c.setAttr(200) # Call parent class method again
c.getAttr() # Call the method of the parent class again

 4. Class properties and methods

1) Private properties of the class:

__private_attrs: Start with two underscores, declare that the attribute is private and cannot be used or directly accessed outside the class. When using self.__private_attrs in a method inside a class.

2) Method of the class

Inside the class, use the def keyword to define a method for the class. Unlike the general function definition, the class method must contain the parameter self, which is the first parameter

3) Private methods of the class:

__private_method: Starts with two underscores, declaring that the method is a private method and cannot be called outside the class. Calling self.__private_methods inside the class

 

class JustCounter:
    __secretCount = 0 # private variable
    publicCount = 0 # public variable
 
    def count(self):
        self.__secretCount += 1
        self.publicCount += 1
        print self.__secretCount
 
counter = JustCounter()
counter.count()
counter.count()
print counter.publicCount
print counter.__secretCount # Error, instance cannot access private variables

5. Instructions for single underline, double underline, and double underline at the head and tail:

1) __foo__: defines a special method, similar to __init__().

2) _foo: A variable starting with a single underscore indicates a variable of the protected type, that is, the protected type can only be accessed by itself and its subclasses, and cannot be used for from module import *

3) __foo: Double underscores indicate variables of private type (private), which can only be accessed by the class itself.

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646592&siteId=291194637