Distinguishing attribute name from varible name to make codes clear and definite
luofeiyu
elearn2014 at gmail.com
Thu Aug 21 10:13:32 EDT 2014
I feel that self.x and x will be confused in the following codes.
class MyDescriptor(object):
def __init__(self, x):
self.x = x
def __get__(self, instance, owner):
print('get from descriptor')
return self.x
def __set__(self, instance, value):
print('set from descriptor')
self.x = value
def __delete__(self, instance):
print('del from descriptor, the val is', self.x)
exam=MyDescriptor("hallo")
when class MyDescriptor initiate , the `hallo` was passed into x in
__init__(self, x);
Literally self.x maybe understood to be self.hallo ,assign a attribute
named 'hallo' to instance exam.
It is a better way to replace self.x to self.y .
class MyDescriptor(object):
def __init__(self, x):
self.y = x
def __get__(self, instance, owner):
print('get from descriptor')
return self.y
def __set__(self, instance, value):
print('set from descriptor')
self.y = value
def __delete__(self, instance):
print('del from descriptor, the val is', self.y)
exam=MyDescriptor("hallo")
There is a attribute y in instance exam , the `hallo` was passed into x
in __init__(self, x).No any relation between x and y ,`hallo` and y.
My view is correct or not ?
More information about the Python-list
mailing list