Get the App
SLTechnology News&Howtos  ›  Development  › 

How to convert python to Private property

Shulou Source: shulou.com Published: 2022-06-03 10:55:21 09月16日 Update

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.

Tags: Properties methods private article underline manual more good practical two content name example article is in knowledge rules Xiao Ming Xiao Hong reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Microsoft Huawei vpn macOS