[ python-Feature Requests-985094 ] getattr(object, name) accepts only strings

SourceForge.net noreply at sourceforge.net
Sun Jan 2 01:29:50 CET 2005


Feature Requests item #985094, was opened at 2004-07-05 01:21
Message generated for change (Comment added) made by complex
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=985094&group_id=5470

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Viktor Ferenczi (complex)
Assigned to: Nobody/Anonymous (nobody)
Summary: getattr(object,name) accepts only strings

Initial Comment:
getattr(object,name) function accepts only strings. This behavior prohibits some interesting usage of callables as names in database column and field references.

For example:

Someone references a database field:
value=record.field_name

Programmatically, when name is the name of the field:
value=getattr(record,field_name)

Calculated fields could be implemented by passing a callable as a field name:

def fn(record): return '%s (%s)'%(record.name,record.number)

value=getattr(record,fn)

The database backend checks if the name is callable and then call the name with the record.

But this cannot be implemented in the simple way if getattr checks if the name is a string or not. This is an unneccessary check in getattr, setattr and delattr, since prevents interesting solutions.

Temporary workaround:

value=record.__getattr__(fn)

There can be many unneccessary type checks and limitations in core and library functions. They should be removed to allow free usage.

----------------------------------------------------------------------

>Comment By: Viktor Ferenczi (complex)
Date: 2005-01-02 01:29

Message:
Logged In: YES 
user_id=142612

Thanks for your comment. I know about property, of course. I
had to change an old application where reimplementing
everything with properties could be dificult. The problem
has been solved for now. I use SQLObject (www.sqlobject.org)
for new applications, whereever possible. Usage of other
Object-Relational mappings (or even ZODB) are under
consideration. - Viktor

----------------------------------------------------------------------

Comment By: Armin Rigo (arigo)
Date: 2004-12-23 23:55

Message:
Logged In: YES 
user_id=4771

This is in part due to historical reasons.  I guess you know 
about "property"?  This is exactly what database people 
usually call calculated fields.

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=355470&aid=985094&group_id=5470


More information about the Python-bugs-list mailing list