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

How to convert python to Private property

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

Share

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

This article will explain in detail how to convert python into a private attribute. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

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 fall, but please follow the convention rule print (xiao_hong._age) on" how to convert python into private attribute "this article ends here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.

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