[prog] Problem with objects in Python3

Isabelle Hurbain-Palatin isabelle at palatin.fr
Wed Mar 30 18:56:34 UTC 2022


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