Python learning notes (36-40) classes and objects

Keywords: Python Attribute Programming

Learning topics: classes and objects
Study date: February 9, 2020
Python version: 3.7.4
Learning note: it's very difficult here, especially the first contact with the concept of object-oriented. What I wrote below is not very clear.

Lesson 36 concept of object
How to describe an object.
For example, this object is a dog
The basic characteristics of a dog are: a head, four legs, hair, tail, etc
What else can a dog do? He can run, eat, bite, smell, etc.

To summarize:
Object = attribute + method. It may be more accurate to use Class here. This is a Class, Class.
The Chinese meaning of class is: birds of a feather flock together. A group of people. What's more, people with the same characteristics, such as drunkards? Yes, such people love drinking. They have the property of loving drinking. They have the ability to drink.
In fact, the concept of object is very wide. In Python, a function, a method, a variable and so on can be called an object. So, Python is full of objects.

  • Define a class
#The following defines the dog class
#Remember: in Python, the first letter of a class name should be uppercase, and the first letter of a function should be lowercase, so it's easy to distinguish
class  Dog:
    #attribute
    legs=4
    head=1
    mouth=1

    #Method
    def beat(self):
        print('dog can beat ')
    def run(self):
        print('dog can run')
    def smell(self):
         print('dog can smell')
  • Call class
>>> 
 RESTART: C:/Users/SNIPER/AppData/Local/Programs/Python/Python37/SniperPyWorks/game4.py 
>>> d=Dog()
>>> d
<__main__.Dog object at 0x0000000002F65B08>
>>> type(d)
<class '__main__.Dog'>
>>> d.legs
4
>>> d.beat
<bound method Dog.beat of <__main__.Dog object at 0x0000000002F65B08>>
>>> d.beat()
dog can beat 
>>> 

What is OO?
That is, the meaning of original object

Characteristics of OO: encapsulation, inheritance, polymorphism.

#Inheritance example
>>> class list1(list):
	pass
>>> list2=list1()
>>> list2.append(1)
>>> list2
[1]
#Give an example of polymorphism
#Define a Class A
>>> class A:
	def fun(self):
		print('i am A')

#Define a class B		
>>> class B:
	def fun(self):
		print('i am B')

		
>>> a=A()
>>> b=B()
>>> a.fun()
i am A
>>> b.fun()
i am B
#It is found that the above function is also a fun function, but the results are different, so this is polymorphism

Lesson 37 object oriented programming

What is self?
Python's self is equivalent to the C++ self's this pointer
Public and private?
Python is pseudoprivate

Lesson 38 inheritance
Let the similar pass without rewriting. This is called inheritance.
Inherited is called: base class (or parent class, superclass)
Inheritor: subclass
The usage is as follows:

#DerviedClassName subclass
#BaseClassName base class (or parent class, superclass)
class DerviedClassName(BaseClassName):
    ...

Raise a chestnut.

>>> class Parent:
	def hello(self):
		print('Calling method of parent class...')

		
>>> class Child(Parent):
	pass

>>> p=Parent()
>>> p.hello()
//Calling method of parent class...
>>> c=Child()
>>> c.hello()
//Calling method of parent class...
>>> 
>>> class Parent:
	def hello(self):
		print('Calling method of parent class...')

>>> class Child(Parent):
	def hello(self):
		print('Calling method of subclass')

		
>>> 
>>> c=Child()
>>> c.hello()
//Calling method of subclass

Use of super function.

Use of multiple inheritance.

Lesson 39 some concepts

combination

>>> class Classmate:
	def __init__(self,y):
		self.num=y

		
>>> class Teacher:
	def __init__(self,x):
		self.num=x

		
>>> class Classroom:
	def __init__(self,x,y):
		self.teacher=Teacher(x)#instantiation
		self.classmate=Classmate(y)#instantiation
	def print_num(self):
		print('There are teachers in the classroom %d Name, student %d name'% (self.teacher.num,self.classmate.num))

>>> classroom=Classroom(1,40)
>>> type(classroom)
<class '__main__.Classroom'>
>>> classroom.print_num
<bound method Classroom.print_num of <__main__.Classroom object at 0x0000000002FAA988>>
>>> classroom.print_num()
//There are teachers in the classroom 1 Name, student 40 name

The above is an example of a combination. It is to instantiate several classes without inheritance relationship, generally horizontal parallel relationship.

Class, class object, instance object

#C is the class definition
>>> class C:
	num=0
#a. B is the instance object, and C below is the class object
>>> a=C()
>>>> a.num
0
>>> a.num=10
>>> a.num+=5
>>> a.num
15
>>> b=C()
>>> b.num
0

If the properties and methods are the same, what's the problem

>>> class C:
	def x(self): #x method
		print('x!!')
		
>>> c=C()
>>> c.x()
x!!
>>> c.x=1
>>> c.x #View properties
1
>>> c.x()#View method
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    c.x()
TypeError: 'int' object is not callable
#z that's wrong

Therefore, pay attention to:

  • Instead of trying to define all the features and methods you can think of in a mine, you should use inheritance and composition mechanisms to expand.
  • Name with different parts of speech, such as attribute name with noun, method with verb.

binding
Python strictly requires that methods need instances to be called. This limitation is actually what Python calls the binding concept.

Lesson 40 some related BIF S

#issubclass(class,classinfo)
>>> class A:
	pass

>>> class B(A):
	pass


>>> issubclass(B,A)
True


>>> class C:
	pass

>>> issubclass(B,C)
False
#hasattr (object,name) tests whether there is an attribute in an object
#Note the full name of attr is attribute, which means attribute


#getattr(object,name[,default])
#setattr(object,name,value)
#property

81 original articles published, 57 praised, 10000 visitors+
Private letter follow

Posted by mikeabe on Tue, 11 Feb 2020 01:01:17 -0800