A __getattr__ for class methods?

Dylan Moreland dylan.moreland at gmail.com
Wed Feb 8 12:54:53 EST 2006


I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

    Person.find_by_name
    Person.find_by_city_and_name
    Person.find_by_name_and_city_and_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, or am I trying to mirror a functionality that Python simply does
not have?




More information about the Python-list mailing list