[Texas] Fwd: Trouble with one of the Koans

Mark Sharp msharp at sfbr.org
Sat Sep 4 05:37:53 CEST 2010


I neglected to place the Texas Python User Group in the Cc: portion of the header in my response to Alex Robbins' helpful note. I needed to tell you that my question was answered and Alex deserves the credit for being both helpful and articulate.

Mark

Begin forwarded message:

From: Mark Sharp <msharp at sfbr.org<mailto:msharp at sfbr.org>>
Date: September 3, 2010 10:33:15 PM CDT
To: Alex Robbins <alexander.j.robbins at gmail.com<mailto:alexander.j.robbins at gmail.com>>
Subject: Re: [Texas] Trouble with one of the Koans

Alex,

I noticed your message come in while I was working on another project and just now got back to playing with Python. I had been looking forward to reading your message all evening. I was not disappointed. You provided a wonderful explanation. I pasted your description into my version of the koan for future reference. Well written with a very clear example. Thank you very much.

Mark
On Sep 3, 2010, at 4:06 PM, Alex Robbins wrote:

I think the basic idea here is that you can define a __setattr__
method on your object, which is later used by python for assignments.

The __setattr__ method gives you more control over assignment into
your object. This __setattr__ basically says:

If the attr_name the user is trying to assign to ends with comic, then
prepend my_ to the attribute name.
If the attr_name the user is trying to assign to ends with pie, then
prepend a_ to the attribute name.
Then assign the provided value to the newly computer attribute name.

instance = PossessiveSetter()
instance.comic = 'Texas'

At this point, if I lookup instance.comic, nothing is there. The
__setattr__ method intercepted my assignment and put it into my_comic.

instance.my_comic == 'Texas'
True


Does that make any more sense than the koan?
Alex

On Fri, Sep 3, 2010 at 9:57 AM, Mark Sharp <msharp at sfbr.org<mailto:msharp at sfbr.org>> wrote:
In the about_attribute_access.py of the Python 3.1 koans, the following
class is defined. Although I can get the tests to pass, I am missing the
point.

Do you know of any discussion of the koans? I have not located one.
Alternatively can you direct me to someone that would be willing to explain
what this class is teaching?

Mark

  class PossessiveSetter(object):
      def __setattr__(self, attr_name, value):
          new_attr_name =  attr_name

          if attr_name[-5:] == 'comic':
              new_attr_name = "my_" + new_attr_name
          elif attr_name[-3:] == 'pie':
              new_attr_name = "a_" + new_attr_name

          object.__setattr__(self, new_attr_name, value)

  def test_setattr_intercepts_attribute_assignments(self):
      fanboy = self.PossessiveSetter()

      fanboy.comic = 'The Laminator, issue #1'
      fanboy.pie = 'blueberry'

      self.assertEqual(__, fanboy.a_pie)

      prefix = '__'
      self.assertEqual("The Laminator, issue #1", getattr(fanboy, prefix +
'_comic'))



R. Mark Sharp, Ph.D.
Director of Primate Records Database
Southwest National Primate Research Center
Southwest Foundation for
Biomedical Research
P.O. Box 760549
San Antonio, TX 78245-0549
Telephone: (210)258-9476
e-mail: msharp at sfbr.org<mailto:msharp at sfbr.org>






_______________________________________________
Texas mailing list
Texas at python.org<mailto:Texas at python.org>
http://mail.python.org/mailman/listinfo/texas




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/texas/attachments/20100903/ff128edc/attachment-0001.html>


More information about the Texas mailing list