Re: [Edu-sig] Attribute Lookup Order

2009-02-07 Thread David MacQuigg
At 11:54 AM 2/6/2009 -0800, Scott David Daniels wrote:

About my message:
 ...  Nickel summary, lookup order is not dirt simple, and reading
 and writing work differently.

David MacQuigg wrote:
Maybe I'm missing some subtleties, but it still seems simple to me.
An attribute is looked up in the order object - class - superclasses,
*UNLESS* that attribute happens to be a property of the class

The subtleties involve the difference in the lookup order between
read-only properties and properties with a write method.

...
class Rectangle(object):

def __init__(self, width, height):
self.width = width
self.height = height
#self.area = width * height

def getArea(self):
return self.width * self.height

area = property(getArea)
...

Written in later versions of Python (2.5 and up) as:

class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
# self.area = width * height

@property
def area(self):
return self.width * self.height

Nice!  Syntax sugar for the more explicit form.  I'll update my examples.

As for the subtle changes in lookup order, I could include some discussion 
under Advanced Topics (Metaclasses, Cooperative Super Calls, etc.), but I will 
need to see a clear example that is relevant to the non-programmer scientists 
and engineers I am targeting.  Else, the value is too low and the complexity 
too high.

The @property example above *is* appropriate for my OOP chapter (under the 
topic Robust Programming).  It is simple, and understanding the concept of 
protected attributes is good even if you don't use them.

-- Dave



___
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig


Re: [Edu-sig] Attribute Lookup Order

2009-02-06 Thread David MacQuigg

And I just noticed a great thread on comp.lang.python addressing the
exact lookup order for obtaining attributes most recent post this
morning.  The Thread Title is Understanding  Descriptors,
Brian Allen Vanderburg II asked the initial question, and Aahz and
Bruno Desthuillers came up with a thorough answer.
I'll put a link here, but it may wrap nastily.  Nickel summary, lookup
order is not dirt simple, and reading and writing work differently.

http://groups.google.com/group/comp.lang.python/browse_thread/thread/a136f7626b2a8b7d/70a672cf7448c68e

Maybe I'm missing some subtleties, but it still seems simple to me.  An 
attribute is looked up in the order object - class - superclasses, *UNLESS* 
that attribute happens to be a property of the class.  The purpose of 
properties is to over-ride direct access to attributes, and substitute your own 
getters and setters with whatever robustness is appropriate.  Say you have a 

class Rectangle(object):

def __init__(self, width, height):
self.width = width
self.height = height
self.area = width * height

and you find your students are changing the width and height parameters, but 
forgetting to change area.  The Java guys will say we should never have written 
such fragile code, but that is the beauty of Python.  We can write something 
simple, and add the robustness later.

class Rectangle(object):

def __init__(self, width, height):
self.width = width
self.height = height
#self.area = width * height

def getArea(self):
return self.width * self.height

area = property(getArea)

Nothing in the interface changes.  Programs dependent on Rectangle will not 
break, unless they were setting area directly, which they should never have 
done.  Then when they do break, the user will see AttributeError: can't set 
attribute, not some subtle error in the data that will screw up later usage.

The example is from Martelli's Nutshell, 2nd ed. p.100-101.  Quick summary 
(quoting Martelli): The crucial importance of properties is that their 
existence makes it perfectly safe and indeed advisable for you to expose public 
data attributes as part of your class's public interface.

-- Dave 


___
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig


Re: [Edu-sig] Attribute Lookup Order

2009-02-06 Thread Scott David Daniels

About my message:
 ...  Nickel summary, lookup order is not dirt simple, and reading
 and writing work differently.

David MacQuigg wrote:

Maybe I'm missing some subtleties, but it still seems simple to me.

 An attribute is looked up in the order object - class - superclasses,
 *UNLESS* that attribute happens to be a property of the class
The subtleties involve the difference in the lookup order between
read-only properties and properties with a write method.


...
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
#self.area = width * height

def getArea(self):
return self.width * self.height
...


Written in later versions of Python (2.5 and up) as:
class Rectangle(object):
def __init__(self, width, height):
self.width = width
self.height = height
# self.area = width * height

@property
def area(self):
return self.width * self.height


--Scott David Daniels
scott.dani...@acm.org

___
Edu-sig mailing list
Edu-sig@python.org
http://mail.python.org/mailman/listinfo/edu-sig