python实例方法的使用注意

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学习教程请关注IT培训机构:千锋教育。

申请14天超长免费试听资格
获取500G教程资料
姓名
电话
课程
立即申请