[prog] Problem with objects in Python3

David Sumbler david at aeolia.co.uk
Wed Mar 30 20:05:24 UTC 2022


Thanks for your swift reply.  And yes, that did the trick.  The
dictionary is good, because the keys can be referenced by a variable
without me needing to know what the actual keys are.

My program now references the data in the way I want.  All I have to do
now is process the data in a suitable manner and write an output file.

Thank you for your help.

David


On Wed, 2022-03-30 at 20:56 +0200, Isabelle Hurbain-Palatin wrote:
> Hi David,You're probably looking for a dictionary - 
> https://docs.python.org/3/library/stdtypes.html#typesmapping
> It allows you to store "content" (your Person data object) according
> to "keys" (that person's name) with syntax such as 
> 
> persons = {}
> persons['John Doe'] = Person(....)
> if ('John Doe' in persons):
>    doStuff()
> 
> I haven't coded in Python in quite some time, so you'll have to
> excuse me if my syntax is wrong - but that's what you're looking for,
> I think.
> 
> Cheers!
> Isabelle
> On Wed, Mar 30, 2022 at 8:45 PM David Sumbler <david at aeolia.co.uk>
> wrote:
> > I have used Python quite a bit for odd projects in the past, but I
> > am
> > 
> > certainly nowhere near being an expert (nor ever likely to be).
> > 
> > 
> > 
> > The problem I am struggling with at the moment is this: the program
> > I
> > 
> > am working on reads a file which contains data about a number of
> > 
> > people.  Each line has data about just one person (including
> > her/his
> > 
> > name), and normally there will be a number of other lines with
> > 
> > different data about that same person.
> > 
> > 
> > 
> > What I want to do is to use the person's name (with spaces removed)
> > as
> > 
> > a variable name, and create a Person object using that name.
> > 
> > 
> > 
> > My problem is that I can't hard code the variable name because it
> > is
> > 
> > derived from data in the input file.  With a hard-coded name I can
> > 
> > easily check whether the object exists, and create it if it
> > doesn't;
> > 
> > but I can't figure out how to do this with names that are derived
> > from
> > 
> > data in a file.
> > 
> > 
> > 
> > Can somebody point me in the right direction?
> > 
> > 
> > 
> > David
> > 
> > _______________________________________________
> > 
> > Programming mailing list
> > 
> > Programming at linuxchix.org
> > 
> > https://mailman.linuxchix.org/mailman/listinfo/programming
> > 




More information about the Programming mailing list