class_dict = {} exec(""" country = "China" def __init__(self,name,age,sex): self.name = name self.age = age self.sex =sex def say_hello(self): print('%s 说:你好!' %self.name) """, globals(), class_dict) my_class = type('my_class', (object,), class_dict) m = my_class('小明', 12, '男') m.say_hello()