首页 / 知识

python实例方法的使用注意

2023-11-12 12:59:00

python实例方法的使用注意

1、实例方法是从属于实例对象的方法,定义实例方法时,第一个参数必须为self。self指当前的实例对象。

2、调用实例方法是,不需要也不能给self传值,self由解释器自动传参。

实例

classgetMin():

#实例方法

deffun(self,arr,n):

print(arr[n-1])

#类方法

@classmethod

defclass_fun(cls):

print("thisisclassfunction")

if__name__=="__main__":

arr=input().strip().split("")

int_arr=[]

foriteminarr:

int_arr.append(int(item))

n=int(input())

instance=getMin()

#用实例调用实例方法

instance.fun(int_arr,n)

#用类调用方法

getMin.fun(instance,int_arr,n)

#实例调用类方法

instance.class_fun()

#类调用类方法

getMin.class_fun()

以上内容为大家介绍了python培训之实例方法的使用注意,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注我们

培训实例方法定义对象

最新内容

相关内容

热门文章

推荐文章

标签云

猜你喜欢