Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76738
| Date | 2014-08-21 22:13 +0800 |
|---|---|
| From | luofeiyu <elearn2014@gmail.com> |
| Subject | Distinguishing attribute name from varible name to make codes clear and definite |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13252.1408630431.18130.python-list@python.org> (permalink) |
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 ?
Back to comp.lang.python | Previous | Next — Next in thread | Find similar | Unroll thread
Distinguishing attribute name from varible name to make codes clear and definite luofeiyu <elearn2014@gmail.com> - 2014-08-21 22:13 +0800 Re: Distinguishing attribute name from varible name to make codes clear and definite Denis McMahon <denismfmcmahon@gmail.com> - 2014-08-21 14:23 +0000
csiph-web