Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of functions in python object-oriented

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of python object-oriented functions, the article is very detailed, has a certain reference value, interested friends must read it!

object-oriented

Object-oriented programming-Object Oriented Programming, referred to as OOP, is a kind of programming idea. OOP regards an object as the basic unit of a program. An object contains data and functions that manipulate data.

Class Car: # static field, class attribute one of the types of cars=' cars' def _ _ init__ (self,name): # dynamic field, instance property self.name=namecar1=Car ('BMW') print (car1.name) print (Car.cars) print (car1.cars)

Define 1 class: class xxx:

Initialize 1 object: def _ _ init__ (self,value)

_ _ init__ means initialization

Define the Car class, while the BMW instance object has the name property

Through the dictionary, all the attributes of the instance object are fetched at once _ _ dict__

Class People: def _ _ init__ (self,name,sex,number,age): self.name=name self.sex=sex self.number=number self.age=agep1=People ('SBharmel','man',1350023,16) print (p1. Through dictionary, all attributes of the object are retrieved at once # output result: # {' name': 'SBharmel',' number': 1350023,' sex': 'man',' age': 16}

Subordinate relationship

Cars belongs to the Car class and cannot access dynamic fields

Name belongs to the car1 object object that can access dynamic and static fields

Only a dependency can be obtained by using the .xxx method.

So you can write car.cars # to get static fields, car1.name # to get dynamic fields.

If it is written as car.name, it will report an error.

The reason why you can't write:

1. The subordination is different.

two。 There may be many types of cars, such as BMW and Mercedes-Benz, which have different properties, such as price and speed.

# but the object can get the static field car1.cars

Because car1 also belongs to this class (car)

Class car: def _ init__ (self,name,speed,price): self.name=name self.speed=speed self.price=pricecar1=car ('BMW', '130km Compact') car2=car ('Mercedes-Benz', '140km Grey h') '80w') print (car1.name) # correct print (car2.price) # correct print (car.speed) # error' 'Traceback (most recent call last): File "E:\ workspace\ day4\ backend\ class.py", line 23, in print (car.speed) AttributeError: type object' car' has no attribute 'speed''''

Static and dynamic fields

The fields created by the class are called static fields cars

The field created by .sefl is called the dynamic field name

What def () is written in a class is called a method, and what is written outside a class is called a function.

A method that is accessed directly by a class or object name.

The function () constructed by @ staticmethod # does not pass the parameter self, and can be called by both classes and objects.

A method that is directly accessed by a class or object name.

The function () constructed by @ classmethod # does not pass the parameter self, but to write cls, call write (), both classes and objects can call

Characteristics

The parameter self is passed in the function () constructed by @ property #. The call does not need to be written (). Only the object can be called.

# is only the characteristic of the instance object, and the class call has no content, which is equivalent to instantiating the unique attribute.

Class Student: # static field, class attribute classtype = "student" # initialize instance property, dynamic field def _ _ init__ (self,name,age): self.name = name self.age = age # dynamic method Instance method def eat (self): print ("students are eating") # static method @ staticmethod def staticfun (): print ("this is a static method") # class method @ classmethod def cm (cls): print ("this is a class method") # feature @ property def properfun (self): print ("properfun: my feature, is a student Advise you to take care of yourself ") print ("-classes that can be called-") print (Student.classtype) Student.staticfun () Student.cm () Student.properfun # does not report errors, but does not print Student.properfun () reports an error print (Student.__dict__) print ("- class capacity that can be called after the object is instantiated -") stu1 = Student ("lee") 18) print (stu1.classtype) print (stu1.name) print (stu1.age) stu1.eat () Student.eat (stu1) stu1.staticfun () stu1.cm () stu1.properfun # feature call without adding () It is equivalent to the printed result of an instantiated property print (stu1.__dict__)''--the class capacity that can be called by the class-- this is a static method, and this is a class method {'_ module__':'_ main__', 'classtype':' student' '_ init__':,' eat':, 'staticfun':,' cm':, 'properfun':,' _ _ dict__':,'_ _ weakref__': '_ _ doc__': None}-the class capacity that can be called after instantiating the object-- the student lee18 student is eating this is a static method, this is a class method properfun: my feature Is a student, advise you to take care of yourself {'name':' lee', 'age': 18}' 'the above is all the content of this article "what is the use of functions in python object-oriented". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report