Python private property Set and get Method 2

Source: Internet
Author: User

class Person (object):
Count = 0 #类属性
def __init__ (self,name,age): #实例属性
# Self.__name = Name#name is a private property; The private property receive is the Set/get method;
# self.__name can also be used with Self.setname (name) The advantage is that when creating an instance, it is not necessary to re-assign (call the Set method), the initialization can be assigned to the value
self. SetName (name)
Self.age = Age
def run (self):
Print (Self.__name + "Runaway")
def setName (self,name): #注意: A set method of a private property
Self.__name = Name
def getName (self): # A private property a Get method
Return Self.__name

p = person ("123", 12)
# P.setname ("123")
Print (P.getname ())
Attention:
When setting the object properties (__init__ () method): The difference between Self.__name and Self.setname ()
Self.__name means that the SetName method is not filtered when the value is initialized, and is also called when the value is re-assigned.
Self.setname () is called by the SetName method to filter the initialization value.

Python private property Set and get Method 2

Related Article

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.