Re: [Tutor] Use iterator to refer to an object's attribute?
by Alan Gauld other posts by this author
Apr 20 2006 8:31AM 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?
> Dictionaries are only pairs of data. I assume a list can be one of
> those elements, but I couldn't figure out how to make it work in the
> structure I presented.
Yes, the object that is stored can be anything. Thus
> >> numList = [1,2,3]
> >> chrList = ['1','2','3']
> >> numDict = {}
> >> numDict['asNum'] = numList
> >> numDict['asChr'] = chrList
> >> for item in numDict['asNum']: print item,
...
1 2 3
> >>
shows two lists being stored in a dictionary.
You can also store instances of classes or functions
or even the classes themselves!
> 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?
> > Why not write constructors that take a list as a parameter.
> > The constructor can manage its own list and the higher
> > level constructor just passes in the appropriate list. That way
> > each class only needs to know about the data at the level
> > it is responsible for. So the Module class might look a bit like:
> >
> > class Module:
> > def __init__(self, name, componentlist):
> > self.components = {}
> > self.name = name
> > for component in componentlist:
> > self.components[component[0]] = Component(component)
> >
> > This implies that the component list is structured as a list of tuples.
>
> I originally had tuples, but you can't access individual elements.
What makes you think so?
> >> t = (1,2,3)
> >> print t[0]
1
> >>
The only thing you can't do is alter the data in the tuple - which is
exactly the behaviour you want if reading the tuple from a config file!
> >> 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.
> > but I think its easier and clearer in this case to use:
> >
> > db.Modules[shortmod]
>
> If Modules is a class name, how am I able to call it like that?
Modules is a dictionary of Modules in the db object. I
should probably have used the example from my Module
class above:
myModule.components[compname]
> "AttributeError: Database instance has no attribute 'Modules'"
You will need to modify the definition of the database class
to have the Modules dictionary first! :-)
Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
_______________________________________________
Tutor maillist - Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Ron Britton
Kent Johnson
Ron Britton
Alan Gauld
Alan Gauld
|