0

I have created a (somewhat) conversational bot and I want it to be able to create a model of the people whose names it hears. I wish to do this by having it create a new instance of my "person" class, and have that class instance named as that person (as in Bob, Alice, etc.) I've been at this all night, thought I had it because it said I had created the instance, but when trying to reference said instance of the class, it tells me that the name 'Bob' is not defined:

properNouns = [word for word,pos in taggedSent if pos == 'NNP']

if properNouns:
    npn = str(properNouns)
    print(npn)    # ['Bob']
    npn = re.sub('[\[\]\,\']', '', npn)
    print(npn)    # Bob
    newPerson = Self(npn,'','','','','','','','','','','','','','')  # Put "Bob" as the fName
    newPerson.__name__ = npn  # Change the instance name to "Bob" (well, try anyway)
    print(str(newPerson.__name__) + " was created!")   # Bob was created!
    print(Bob.fName + " is the first name")  # NameError:  name 'Bob' is not defined

My base class is as follows:

class Self():     # A starter concept of a Host's concept of Self/Me
    def __init__(self, fName, lName, DoB, job, hColor, eColor, sex, homeTown, homeState, whatIHave):
        self.fName = fName
        self.lName = lName
        self.DoB = DoB
        self.job = job
        self.hColor = hColor
        self.eColor = eColor
        self.sex = sex
        self.homeTown = homeTown
        self.homeState = homeState
        self.whatIHave = whatIHave
    def getFullName(self):
        fullName = str(self.fName) + ' ' + str(self.lName)
        return fullName.title()

I can successfully manually create an instance and subsequently call up its attributes via dot notation if I hard-code it in advance, but that is not how this needs to work (If in the conversation I say I met with Bob, I need the bot to create a new instance of the Self class named Bob.)

mySelf = Self('Bob','Jones',"Coder",'19730101',black','brown','F','Boston','Massachusetts','a Jeep Wrangler')

And if I subsequently print a Bob.fName, I get Bob....

I can do some fairly decent coding but this is a relatively new area for me, and it may be that I've seen what is the answer but simply don't understand what I'm seeing well enough to know it's what I need, so apologies. I've seen many posts with very similar titles, but they do not seem to have the answer for what I am looking to do here, which seems simple to me: If I create a new "Self" (newSelf = Self), then I want to rename newSelf to "Bob".

Alternatively, and I would think easier, would be to have the new Self created with the proper name to begin with but I can't find a way to take a variable containing the name and make that name = the new Self...

Thanks for your help. I've been at this for over six hours now.

David Xanatos
  • 75
  • 1
  • 9
  • Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). ... there are many othe SO Q&A's on creating variable names dynamically - One common solution is to keep a dictionary with keys of dynamically created strings. – wwii May 13 '18 at 04:10
  • `d={}, d['Bob'] = Self(*args, **kwargs): ...` – wwii May 13 '18 at 04:26

0 Answers0