热线电话:13121318867

登录
2019-03-20 阅读量: 461
python如何自定义类

def test_method(self):

print("This is Test class method!")

# creating a base class

class Base:

def myfun(self):

print("This is inherited method!")

# Creating Test class dynamically using

# type() method directly

Test = type('Test', (Base, ), dict(x="atul", my_method=test_method))

# Print type of Test

print("Type of Test class: ", type(Test))

# Creating instance of Test class

test_obj = Test()

print("Type of test_obj: ", type(test_obj))

# calling inherited method

test_obj.myfun()

# calling Test class method

test_obj.my_method()

# printing variable

print(test_obj.x)

输出:

Type of Test class:  <class 'type'>
Type of test_obj: <class '__main__.Test'>
This is inherited method!
This is Test class method!
atul
16.1242
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子