Access control for properties of classes in Python

Source: Internet
Author: User

Because I'm doing it. NET, have learned Python before, like the language of many features, and recently looked at a moment, to share their feelings to everyone, which will inevitably use C # angle to see the syntax of Python, mainly in the Python class in the access to properties.

When it comes to property access, you can think of public, private, protected, C # methods that are publicly private protected,python and really make you feel difficult to understand.

First define a class (the blog park does not color the Python code, you see the picture bar)

  

Personal knowledge, not necessarily, think Python is a dynamic language, many properties are dynamic plus go, such as the Name property, when called to the SetName method, the rooms class instance will have the Name property, as in the figure, if after the SetName call, Did not go to call SetSize, and in the display method to use the Size property, then the program error, where the individual to make this property public, because instances of the class can actually access.

One more picture.

  

As in the name and location property, in the constructor __init__ passed in, assign values to these two properties, note that self, in fact, now I do not know what name and location is the property, they can be instance properties, can be class properties, Using the variable NB and SZ can be accessed, using the class name City can also be method to, but with the class name access to an empty string. If you change something,

  

In the Setregion method, use city. To find the region and assign it a value, why use NB and SZ can also find this region, and the value is the same, is not a bit like C # static property, is accessed using the class name, but in Python can also use the instance to access , because both classes and instances have their own scope, and if they are to find a property on the instance scope, they are used to go up to the class scope if they cannot find it. Naturally, you can imagine that in the City class, Name,location,region belongs to the class scope, but the name and location properties exist in the instance scope.

You can simply say that the properties in a class scope are accessible to all instances, and other instances are affected when modified, but because the scope of the instance is not found and the class scope is found.

Now the set of static languages is really uncomfortable, but after referencing the scope, the access behavior of Python still knows the point.

Let's say private.

  

Use two underscores to indicate that this is a private property, that the private property also covers the private properties of the class and the private properties of the instance, and that when the last two print statements are executed, the program errors. The book also mentions that private properties can actually be accessed, and the Python interpreter uses the property name to block instances from accessing those properties.

Access control for properties of classes in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.