python中想动态创建对象应该怎么做?
发布网友
发布时间:2022-05-05 01:50
我来回答
共1个回答
热心网友
时间:2022-04-18 16:38
class BaseObject(object):
def __init__(self, **kwg):
self.__dict__ = dict(kwg)
content = '''Tim.Wang|M|43
Bruce.Wang|M|38
Amay.Song|F|21'''
def ln2obj(ln):
name,sex,age = ln.split('|')
return BaseObject(name=name,sex=sex,age=int(age))
students = map(ln2obj, content.splitlines())