如何创建一个从其参数生成类实例的函数
我是一个python newbie,我正在尝试创建一个函数,该函数使用该函数作为实例的属性的参数返回三个类之一的实例。
class User:
def __init__(self, nick, full_name, reg_date, age, role):
self.nick = nick
self.full_name = full_name
self.reg_date = reg_date
self.age = age
self.role = role
class Staff(User):
def __init__(self, nick, full_name, reg_date, age, role):
super().__init__(nick, full_name, reg_date, age, role)
class Adult(Staff):
def __init__(self, nick, full_name, reg_date, age, role):
super().__init__(nick, full_name, reg_date, age, role)
class Young(Adult):
def __init__(self,nick, full_name, reg_date, age, role):
super().__init__(full_name, nick, reg_date, age, role)
def sign_up(nick, full_name, reg_date, age, role):
nick = (input('Choose a nickname: '))
full_name = (input('Please, introduce your name: '))
reg_date = str(date.today())
age = (input('Please, introduce your age: '))
role = (input('Please, introduce your role (Staff, Adult or Young)'))
valid_roles = ['Staff', 'Adult', 'Young']
exec('nick = role(full_name, reg_date, age, role)
我的目标是,当您在函数中运行符号时,用户将用户引入“ nick”参数的值将是实例名称,角色instancem的类别,而其他参数可作为实例。
例如:
sign_in('nick1', 'John Doe', reg_date, 38, 'Staff')
应该导致这样做:
nick1 = Staff(full_name, reg_date, age)
是我在堆栈溢出中的第一个问题,希望我能清楚地解释自己。谢谢。
I'm a python newbie, and I'm trying to create a function that returns an instance of one of three classes, using the arguments of the function as attributes of the instance.
class User:
def __init__(self, nick, full_name, reg_date, age, role):
self.nick = nick
self.full_name = full_name
self.reg_date = reg_date
self.age = age
self.role = role
class Staff(User):
def __init__(self, nick, full_name, reg_date, age, role):
super().__init__(nick, full_name, reg_date, age, role)
class Adult(Staff):
def __init__(self, nick, full_name, reg_date, age, role):
super().__init__(nick, full_name, reg_date, age, role)
class Young(Adult):
def __init__(self,nick, full_name, reg_date, age, role):
super().__init__(full_name, nick, reg_date, age, role)
def sign_up(nick, full_name, reg_date, age, role):
nick = (input('Choose a nickname: '))
full_name = (input('Please, introduce your name: '))
reg_date = str(date.today())
age = (input('Please, introduce your age: '))
role = (input('Please, introduce your role (Staff, Adult or Young)'))
valid_roles = ['Staff', 'Adult', 'Young']
exec('nick = role(full_name, reg_date, age, role)
My goal is that when you run the sign in function the value the user introduces into "nick" argument will be the instance name, role the class of the instancem, and the other arguments work as the instance.
for example:
sign_in('nick1', 'John Doe', reg_date, 38, 'Staff')
Should result in this:
nick1 = Staff(full_name, reg_date, age)
Is my first question here in Stack Overflow and I hope I explained myself clearly. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要任意做变量。改用词典之类的内容。
输出:
Don't arbitrarily make variables. Use something like a dictionary instead.
Output: