Introduction to the public properties of a class

Source: Internet
Author: User

First, the concept

We mentioned the private properties of the class before, which is not directly accessible in the class. But is the property that can be accessed directly is the public property? Actually, it's not. The properties in the __init__ () constructor are basically accessible to the outside, but they are not public properties. What do you mean by public property?

Definition: Refers to a property that is accessible to all objects belonging to this class, called Public properties.

Second, the attribute

2.1 Member properties

class Person (object):    def __init__ (self, name, job, phone, address):        self.name = name    #  member property, belonging to an instance object C7/>self.job = Job        self.phone = phone        self.__address = Address    def get_private (self):        return self.__ Address    def sayhi (self):        print ("hell,%s"% self.name) P1 = person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') P2 = Person (' Ashlex ', ' Police ', ' 8833232 ', ' BJ ')
Print (P1.job, p2.job) # output Doctor Police

We defined two objects under the person class, p1 and P2. Obviously there's no way we can get P1 to visit P2 's job attribute, or police. The Self.name properties, such as those in the constructor __init__ (), are called member properties.

2.2 Public properties

class Person (object):    nationality = ' CN '   # defines the public attribute    def __init__ (self, name, job, phone, address):        Self.name = name        self.job = Job        self.phone = phone        self.address = Address    def sayhi (self):        print (" hell,%s "% self.name) P1 = person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') P2 = person (' Ashlex ', ' Police ', ' 8833232 ', ' BJ ') prin T (p1.nationality) print (p2.nationality) # output CNCN

For a public property, all instance objects have the same value to access it.

 

Iii. Characteristics of public properties

We can not only access, but also change the public properties. 

class Person (object):    nationality = ' CN '   # defines the public attribute    def __init__ (self, name, job, phone, address):        Self.name = name        self.job = Job        self.phone = phone        self.address = Address    def sayhi (self):        print (" hell,%s "% self.name) P1 = person (' Bigberg ', ' Doctor ', ' 8833421 ', ' Hz ') P2 = person (' Ashlex ', ' Police ', ' 8833232 ', ' BJ ') prin T (person.nationality)  # call Public Property Person.nationality = ' Us '   # Change public property print (person.nationality) #输出CN US

  

3.1 Single instance invocation and change of public properties

# based on the example print ("--------befoer change---------") print (person.nationality) print (p1.nationality) print (p2.nationality) Print ("--------after Change---------") print (person.nationality) p1.nationality = ' JP ' Print (p1.nationality) print ( p2.nationality) # Output

--------Befoer Change---------
CN
CN
CN
--------after Change---------
US
Jp
US

P1 can be well understood before the change, because everyone is called the class person's public property nationality, so P1, P2 The nationality attribute is the same, is ' CN '. But why has the nationality attribute of P2 not changed after P1 has modified the public attribute?

  

When we define a person class, it already exists in memory and, of course, contains the public properties of the class. In the initial instance P1 to invoke the nationality of the class person, it is directly referencing the nationality memory address in the class, rather than adding a property called nationality.

As shown below:

Print (ID (person.nationality)) print (ID (p1.nationality)) print (p2.nationality) print (person.nationality, P1.nationality, p2.nationality) #输出175123683612817512368361281751236836128CN cn cn

This explains why P2 also changes when the nationality in the person class changes to ' US '. Because it is a direct reference to the value in memory.

 

p1.nationality = ' JP '

After P1 directly assigns the value nationality this attribute, actually is the instance P1 for oneself new one member variable, is called nationality. Just their name is the same, but there is no connection between the two, even the memory address is not the same.

# p1.nationality = ' JP ' Print (ID (person.nationality)) print (ID (p1.nationality)) print (ID (p2.nationality)) print ( Person.nationality, P1.nationality, p2.nationality) #输出243457958509624345795851522434579585096US JP US

So p1.nationality= ' JP ' does not modify the public properties of the class person, but instead creates a new member property for itself, so the change of P1 has no effect on the public properties of the class.

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.