Python Beginner's narrow attributes (@property) __python

Source: Internet
Author: User

Python2 and Python3 properties of the processing and implementation is not the same, the internal implementation of a large difference, this article refers to the concept of narrow sense.


I. Attributes in the Python2

Attributes are declared using @property or property in Python2, but attributes are not implemented in real sense and do not provide the proper protection mechanism. When adding new members to an object, the original private variable in the Python2 does not change, but the new member is dynamically added, and the defined attributes are hidden.

>>> class A:
	def __init__ (self,v):
	    self.__v = v
	@property
	def v (self): return
		Self.__v

	
>>> A = A (3)
>>> a.v
3
>>> A.V = 5# Dynamic addition of new members, hidden attributes
>>> A.V
5
>>> a._a__v# original private variable did not change
3
The following code actually adds new members, hiding existing properties
	
>>> class A:
	def __init__ (self,v):
	    self.__v = v
	
	def __get (self): return
		self.__v
	
	def __ Set (SELF,V):
		self.__v = v
	v = property (__get,__set)
	def Show (self):
		print Self.__v
	def show (self ):
		print self.__v

		
>>> t = A (3)
>>> T.V
3
>>> T.V +=2# dynamic Add new Member
>>> t.v# access to new members
5
>>> t.show () #访问私有成员
3
>>> del t.v# Delete new members just added
>>> t.v# access to the original property
3
>>> del t.v# attempted to delete a property

Traceback (most recent call last):
  File "<pyshell#200>", line 1, in <module>
    del T.V
attributeerror:a instance has no attribute ' V ' 
  >>> T.V
3
>>> del t._a__v# Delete Private member
>>> t.v# Access property, but private members no longer exist

Traceback (most recent):
  file "<pyshell#203>", line 1, in <module>
    t.v
  File "< Pyshell#192> ", line 6, in __get return
    Self.__v
attributeerror:a instance has no attribute ' _a__v '

The following shows the relationship between a private member and an ordinary member:
Class A:
	def Show (self):
		print self.a
		print Self.__b

		
>>> a = A ()
>>> a.show ()

Traceback (most recent call last):
  File "<pyshell#210>", line 1, in <module>
    a.show ()
  file "<pyshell#208>", line 3, in
    Show Print SELF.A
attributeerror:a instance has no attribute ' a '
>>> a.a =3
>>> a.show () C13/>3

Traceback (most recent):
  File "<pyshell#212>", line 1, in <module>
    a.show ()
  File "<pyshell#208>", line 4, in Show
    print Self.__b
attributeerror:a instance has no attribute ' _a __b '
>>> a.__b = 4
>>> a.show ()
3

traceback (most recent call last):
  File "& Lt;pyshell#214> ", line 1, in <module>
    a.show ()
  File" <pyshell#208> ", line 4,
    on show Print Self.__b
attributeerror:a instance has no attribute ' _a__b '

>>> a._a__b = 5
>>> A . Show ()
3
5

Two. Properties in Python3

In the Python3, the attributes are implemented in a more complete way, which supports a more comprehensive protection mechanism. If you set the property to read-only, you cannot modify its value or add new members with the same name as the property, and you cannot delete the properties of the object.

(1) Read-only, not modifiable and deleted

Class A:
	def __init__ (self,v):
		self.__v = v
		
	@property
	def v (self): return
		self.__v

	
>> > A = A (3)
>>> a.v
3
>>> a.v = 4# Read-only property not allowed to modify
Attributeerror:cannt Set Attribute

>>> A.VV = 5# Dynamic Add Formation member >>> A.VV 5 >>> del a.vv# dynamic Delete member
>>> del a.v# attempt to delete object failed
>>> A.V
3
# (2) read below, can be modified, cannot be deleted

Class A:
	def __init__ (self,v):
	    self.__v = v
	
	def __get (self): return
		self.__v
	
	def __set (self,v):
		Self.__v = v
	v = property (__get,__set)
	def Show (self):
		print Self.__v
	def show (self):
		print Self.__v
>>> A = A (3)
>>> a.v
3
>>> A.V = 4# Allow modification
>>> A.V
4
>>> a.show () #属性值对应的私有变量也得到了修改
5
>>> del a.v# attempt to delete object failed
# (3) can be read below, can be modified, can be deleted
Class A:
	def __init__ (self,v):
	    self.__v = v
	
	def __get (self): return
		self.__v
	
	def __set (self,v):
		Self.__v = v
	v = property (__get,__set,__del)
	def Show (self):
		print Self.__v
	def show (self):
		Print Self.__v

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.