Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76738 > unrolled thread
| Started by | luofeiyu <elearn2014@gmail.com> |
|---|---|
| First post | 2014-08-21 22:13 +0800 |
| Last post | 2014-08-21 14:23 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.lang.python
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
| From | luofeiyu <elearn2014@gmail.com> |
|---|---|
| Date | 2014-08-21 22:13 +0800 |
| Subject | Distinguishing attribute name from varible name to make codes clear and definite |
| Message-ID | <mailman.13252.1408630431.18130.python-list@python.org> |
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 ?
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2014-08-21 14:23 +0000 |
| Message-ID | <lt4vda$ll5$1@dont-email.me> |
| In reply to | #76738 |
On Thu, 21 Aug 2014 22:13:32 +0800, luofeiyu wrote: > I feel that self.x and x will be confused in the following codes. Then don't call them self.x and x, call them self.internal_x and param_x, or any other pair of different names. You are the one who chooses what names to use in your code. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web