0

I do a journal program, and have I have some teachers, who written as dictionaries. For example:

Joshua_Danice = {'first_name': 'Joshua', 'last_name': 'Danice',
            'supervision': ['9K', '8A', '9B'],
            'subjects': ['biology, chemistry']}

When you try to go in my program, you should print your first name and last one. It is writing itself into a variable "teacher":

def __init__(self, first_name, last_name):
    self.first_name = first_name
    self.last_name = last_name
    self.teacher = str(self.first_name + "_" + self.last_name)

So we have

self.teacher = "Joshua_Danice"

I want to replace that string to the variable, so I can use "Joshua_Danice" string as dictionary, and use keys from this one.

1
  • What's the issue, exactly? Commented Mar 14, 2020 at 17:12

2 Answers 2

2

I would suggest creating a teachers dictionary, which maps the name of the teacher, such as "Joshua_Danice" to the dictionary you post above, e.g.

teachers = {"Joshua_Danice":
    {'first_name': 'Joshua', 'last_name': 'Danice',
     'supervision': ['9K', '8A', '9B'],
     'subjects': ['biology, chemistry']}
}

Then retrieve the inner dictionary using:

teachers["Joshua_Danice"]
Sign up to request clarification or add additional context in comments.

Comments

1

Even though it is not advised to use globals() to check and update the variables of the class, still this might be a hack for you :

Joshua_Danice = {'first_name': 'Joshua', 'last_name': 'Danice',
            'supervision': ['9K', '8A', '9B'],
            'subjects': ['biology, chemistry']}

class Whatever():
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name
        self.teacher = str(self.first_name + "_" + self.last_name)
        if self.teacher in globals():
            self.__dict__.update(globals()[self.teacher])

A better practice would surely be what dspencer suggested.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.