a Python person's experience with Ruby

Steve Howell showell30 at yahoo.com
Sat Dec 8 19:19:50 EST 2007


--- Richard Jones <richardjones at optushome.com.au>
wrote:
> 
> class A(object):
>     def set_a(self, value):
>         self._a = value
>     a = property(lambda self: self._a, set_a)
> 
> Note that this differs from a regular attribute
> because "a" is not deletable
> from instances (the property defines no deleter).
> 

The use case I was actually considering actually
doesn't require much magic in either language.

I commonly want to write code like this:

   e = Employee('jim', 'accounting', 50)
   print e.name
   print e.department
   print e.salary
   e.update_salary(percent=.10)
   e.update_salary(percent=.5)

I want the name, department, and salary always exposed
to me.  But suppose in Employee's innards, it keeps a
cache of the number of raises Jim has been given, and
this cache is very implementation-sensitive.

The way I achieve this is different in each language:

  1) In Python I get name, department, and salary
exposed for free, but I need to make a more conscious
decision to hide e.compensation_tracker, which I do by
renaming it to e.__compensation_tracker.

  2) In Ruby I get compensation_tracker fully
hidden/encapsulated for free, but I have to make a
more conscious decision to expose name, department,
and salary, which I get by saying:

   attr_reader :name, :department, :salary

Obviously, things can get a lot more complicated than
this scenario, and I think both languages are pretty
flexible, but flexible in a different way.










      ____________________________________________________________________________________
Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.  http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ 




More information about the Python-list mailing list