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

Python object-oriented how to create objects and send messages to objects

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

Share

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

This article mainly introduces "Python object-oriented how to create objects and send messages to objects". In daily operation, I believe many people have doubts about how to create objects and send messages to objects in Python object-oriented. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to create objects and send messages to objects by Python object-oriented". Next, please follow the editor to study!

@ property decorator

We have discussed the access rights of properties and methods in Python before. Although we do not recommend making properties private, it is also problematic to expose properties directly to the outside world. For example, we have no way to check whether the values assigned to properties are valid. Our previous suggestion is to start the attribute naming with a single underscore, which implies that the attribute is protected and is not recommended to be accessed directly by the outside world, so if you want to access the attribute, you can do so through the getter (accessor) and setter (modifier) methods of the attribute. If you want to do this, consider using the @ property wrapper to wrap the getter and setter methods, making access to properties secure and convenient, as shown below.

Class Person (object): def _ init__ (self, name, age): self._name = name self._age = age # accessor-getter method @ property def name (self): return self._name # accessor-getter method @ property def age (self): return self._age # modifier-setter method @ age.setter def age (self, age): self._age = age def play (self): if self._age

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