ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-tutor
python-tutor
Re: [Tutor] Use iterator to refer to an object's attribute?
by Ron Britton other posts by this author
Apr 20 2006 11:38AM messages near this date
Re: [Tutor] Use iterator to refer to an object's attribute? | Re: [Tutor] Use iterator to refer to an object's attribute?
> > I wanted to make the methods flexible enough that I wouldn't have to 
> > edit every method if the module list ever changed.  I guess I don't 
> > understand how a dictionary works in this situation.
> 
>  I don;t understand what you don;t understand here. Can you expand on 
>  why you don't think a dictionary would work?

This remark was based on one of my earlier designs.  It didn't work 
there, so I abandoned dictionaries.  The library reference says:
"Only values containing lists, dictionaries or other mutable types 
(that are compared by value rather than by object identity) may not be 
used as keys."
This added to the problem, at least in the design I attempted it in.

> > I originally had tuples, but you can't access individual elements.
> 
>  What makes you think so?
> 
> >>> t = (1,2,3)
> >>> print t[0]
>  1

I didn't work before when I used it as an index.  Now it does.  I must 
have done something different originally.  But you are right.  This 
works:
 > >> mylist = ['one', 'two', 'three']
 > >> t = (0,1,2)
 > >> print mylist[t[0]]
one

> >>> I have object "db.mb".  I have iterator "shortmod" with a value of  
> >>> "mb". Why can't I call "db.shortmod"?
> >>
> >> You can use db.getattr(shortmod)
> > That doesn't work.  It tells me "Database instance has no attribute 
> > 'getattr'".
> 
>  Its actually a special method so needs the underscores __getattr__
>  and accessed via a function. I got my syntax muddled:
> 
>  getattr(db, shortmod)
> 
>  is how it should be written.

That works!  I knew there had to be a way!

Thanks for the help!
Ron

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Ron Britton
Kent Johnson
Ron Britton
Alan Gauld
Alan Gauld

Privacy Policy | Email Opt-out | Feedback | Syndication
© 2004 ActiveState, a division of Sophos All rights reserved