我会尽量在这里回答:
- 您要问的是变量变量名称 - 这不在 Python 中。(我认为它在 VB.Net 中,但不要强迫我这样做)
用户获得一个终端菜单,允许他创建一个新用户并分配姓名、薪水、职位。如果您想管理它、调用函数等,您将如何实例化该对象并稍后调用它?这里的设计模式是什么?
这就是我添加新人的方式(米老鼠示例):
# 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's up to you from here what you want to do
这只是一个基本的粗略示例,但我想你明白我的意思。
另外,如您所见,我在这里没有使用类。类似这样的类几乎总是一个更好的主意,但这只是为了演示如何在运行时更改和使用变量。