关于Python类中,要领的定义体式格局,我们能够归结有4种范例:引荐进修:Python视频教程)
1、不带self、cls参数且不加装潢器(staticmethod、classmethod)
定义代码以下:
class Student(object): def func(name): print('my name is {}'.format(name))
2、一般的要领定义,带self参数
定义代码以下:
class Student(object): def func(self, name): print('my name is {}'.format(name))
3、类要领:加装潢器(classmethod)
定义代码以下:
class Student(object): @classmethod def func(cls, name): print('my name is {} from {}'.format(name, cls.__name__))
静态要领:加装潢器(staticmethod)
定义代码以下:
class Student(object): @staticmethod def func(name): print('my name is {}'.format(name))
更多Python相干技术文章,请接见Python教程栏目举行进修!
以上就是python类中怎样定义要领的细致内容,更多请关注ki4网别的相干文章!