Python Private Property @property method

Source: Internet
Author: User

# Put the resulting property value on the method @property
# on the method of setting the property value @ property name. Setter
# Two methods have the same name and are all property names
#先写 @property Write the @ property name again. Setter
class Person (object):
def __init__ (self,name,age):# When defining object properties, private properties are not added __
Self.name = Name
Self.age = Age
@property#相当于getName () method
def name (self):
return Self.__name# But when you return, you still want to self.__ the private property name

@name. Setter#相当于setName () method
def name (Self,name):
If Len (name) > 6:
Print ("illegal")
Else
Self.__name = Name# When assigning a value also self.__ property name = property name

P1 = person ("312,358", 13)
Print (P1.name)

""
Summary: 1, when defining object properties, do not need to add __ in Object properties
2. To define the @property equivalent to the Get method first
3. Define the @ property name again. Setter equals Set method
4, when the object is called directly is the object name. Property name
‘‘‘
Note: This method sets the private property, and the @ property name is called when the object instance initialization assignment is created. Setter Method Filter Data

Python Private Property @property method

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.