Distinguishing attribute name from varible name to make codes clear and definite

2014-08-21 Thread luofeiyu

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 ?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Distinguishing attribute name from varible name to make codes clear and definite

2014-08-21 Thread Denis McMahon
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, denismfmcma...@gmail.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Distinguishing attribute name from varible name to make codes clear and definite

2014-08-21 Thread Ben Finney
luofeiyu elearn2...@gmail.com writes:

 I feel that self.x and x will be confused in the following codes.

I don't see how. The examples you give have the two quite distinct in
every case.

 exam=MyDescriptor(hallo)

 when class MyDescriptor  initiate , the `hallo` was passed into x in
 __init__(self, x);

That's correct; the argument ‘hallo’ (a text string) is the second
argument in the ‘__init__’ call, and the name ‘x’ is bound to that
value while the function executes.

 Literally  self.x maybe understood to be self.hallo ,assign a
 attribute named 'hallo' to instance exam.

No, the attribute ‘self.x’ is not to be understood as ‘self.hallo’. The
value is not the name.

If the value of the argument were 7.03, the attribute would not be named
‘self.7.03’, it would still be named ‘self.x’.

 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 ?

You are correct that, if you were to name the attribute differently from
the parameter, there would no longer be any clear relationship.

You are, IMO, incorrect that this would help. Rather, the loss of the
clear relationship would make it much more difficult to see why the
relationship exists, and why different names were chosen.

Instead, the programmer is expected to understand that a value is
distinct from any name which is bound to that value.

-- 
 \  “Our products just aren't engineered for security.” —Brian |
  `\ Valentine, senior vice-president of Microsoft Windows |
_o__)development, 2002 |
Ben Finney

-- 
https://mail.python.org/mailman/listinfo/python-list