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

The method of converting python to private property

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "the method of converting python to private attribute". In the daily operation, I believe that many people have doubts about the method of converting python to private attribute. 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 about "the method of converting python to private attribute". Next, please follow the editor to study!

1. Add an underscore before the property or method name to indicate that the property or method is used only in the class.

2. You can manually call _ age to obtain the age attribute. A more stringent method is to add two underscores _ _ before the attribute or method, which cannot be obtained by manually calling the attribute.

Example

Class Student: def _ init__ (self, name, sex, age): self.name = name self.sex = sex self._age = age def run (self): print ("I am {0}, I can run" .format (self.name) def jump (self): print ("I am {0}, {1}) Can jump ".format (self.name, self.sex) xiao_ming = Student (" Xiaoming "," male ", 19) xiao_hong = Student (" Xiao Hong "," female ", 20) xiao_ming.run () xiao_hong.jump () # call private attribute, you can drop it, but please follow the convention rule print (xiao_hong._age), and the study on" how to convert python to private attribute "ends. I hope I can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Internet Technology

Wechat

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

12
Report