Access "slot" field dynamically?

Steven Taschuk staschuk at telusplanet.net
Tue Jun 10 17:26:02 CEST 2003


Quoth Robert Oschler:
  [...]
> In the block belonging to "printSomeFields()", how do I access the field
> (slot) by grabbing the field name from the fldlist list of strings?
> 
>     print instance_of_hasslot.fl

What you need is

    print getattr(instance_of_hasslot, fl)

Btw, I don't think __slots__ is something you should use for your
run-of-the-mill classes.  It's intended for memory optimization
for classes with lots and lots and lots of instances, afaik
(contrary to some of the documentation, which suggests a
bug-prevention role).

-- 
Steven Taschuk                  staschuk at telusplanet.net
"Telekinesis would be worth patenting."  -- James Gleick





More information about the Python-list mailing list