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

SourceForge.net noreply at sourceforge.net
Thu Dec 23 23:55:10 CET 2004


Feature Requests item #985094, was opened at 2004-07-04 23:21
Message generated for change (Comment added) made by arigo
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: Armin Rigo (arigo)
Date: 2004-12-23 22: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