Creating objects at runtime in Python

I have a problem with the concept of OOP when it comes to creating objects at runtime. All the educational code that I reviewed defines specific variables, for example. "Bob" and assigns them to a new instance of the object. Bob = Person ()

Now I am having trouble understanding how I will create a model that creates a new object at runtime? I know that my phrasing is probably wrong, since all objects are generated at runtime, but I mean that if I were to run the application in the terminal or user interface, how could I create new objects and manage them. Can't I define new variable names on the go?

An example application in which I ran into this design problem would be a database in which people are stored. The user receives a terminal menu that allows him to create a new user and assign a name, salary, position. How would you create an instance of this object and call it later if you want to manage it, call functions, etc.? What design template is here?

I apologize for my poor understanding of the OPP model. I am reading lessons and OOP right now, but I feel that I need to understand that my mistake is here before moving on. Please let me know if there is anything that I should clarify.

+3
source share
5 answers

, , /:

class Person(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        print "A person named %s" % self.name

people = {}
while True:
    print "Enter a name:",
    a_name = raw_input()

    if a_name == 'done':
        break

    people[a_name] = Person(a_name)

    print "I made a new Person object. The person name is %s." % a_name

print repr(people)
+7

. .

, - . , , , .

, , , .

+2

.

Person - , :

person = new Person()
person.name = "Bob"
person.email = "bob@aol.com"
person.save()  # this line will write to the persistent datastore (database, flat files, etc)

:

person = Person.get_by_email("bob@aol.com") # assuming you had a classmethod called 'get_by_email'
+1

:

  • , , - Python. ( , VB.Net, )

, , , . , , ..? ?

( Mickey-mouse):

# Looping until we get a "fin" message
while True:
    print "Enter name, or "fin" to finish:"
    new_name = raw_input()
    if new_name == "fin":
        break
    print "Enter salary:"
    new_salary = raw_input()
    print "Enter position:"
    new_pos = raw_input()

    # Dummy database - the insert method would post this customer to the database
    cnn = db.connect()
    insert(cnn, new_name, new_salary, new_pos)
    cnn.commit()
    cnn.close()

, .

while True:
    print "Enter name of employee, or "fin" to finish:"
    emp_name = raw_input()
    if emp_name == "fin":
        break
    # Like above, the "select_employee" would retreive someone from a database
    cnn = db.connect()
    person = select_employee(cnn, emp_name)
    cnn.close()

    # Person is now a variable, holding the person you specified:
    print(person.name)
    print(person.salary)
    print(person.position)

    # It up to you from here what you want to do

, , , .

, , . - , , , .

+1

You would never do Bob = Person()in a real program. Any example that shows that this is probably a bad example; it is essentially hard coding. You often (in real code) do person = Person(id, name)something like that to build an object using the data you received elsewhere (read from a file, obtained interactively from a user, etc.). Even better would be something like employee = Person(id, name).

0
source

Source: https://habr.com/ru/post/1786184/


All Articles