Python静的方法(staticmethod)、クラス方法(classimethod)、__strの使い方
一、使用と特性
1.1、使用説明:
一般的には、ある種類の方法を使うには、まずオブジェクトを具体化してから呼び出す方法が必要です.@staticmethodまたは@classimethodを使うと、実例化を必要とせず、直接にクラス名で呼び出すことができます.
使用:直接類名.方法名() を選択します
1.2、違い:
@staticmethodは、自身のオブジェクトのselfと自身のクラスのclsパラメータを表示する必要はなく、使用関数と同じです.
使用:直接クラス名.属性名 または 直接类名.方法名.直接類名も直接類名()
@classimethodもselfパラメータは必要ありませんが、最初のパラメータは自分のクラスを表すclsパラメータが必要です.
使用:直接クラス名.属性名または 直接クラス名.方法名.
注:両者が定義している装飾器の呼び出し方法は同じです.@staticmethodでこのクラスに呼び出される属性のいくつかの方法は、直接類名.属性名または類名.方法名のみです.また、@classimethodはclsパラメータを持っているので、クラスの属性、クラスの方法、実装オブジェクトなどを呼び出すことができます.
二、静的方法(staticmethod)
通常、クラスで定義されている関数はすべて対象のバインディング方法であり、オブジェクトはバインディング方法を呼び出すと自動的にパラメータとして自分を渡します.この他にも、2つの一般的な方法があります.静的な方法と種類の方法、2つはクラスのためにカスタマイズされていますが、実例は必ず使用しなければなりません.誤報もありません.
は、クラス定義の名前空間に位置しています.具体的なタイプは動作しません.pythonは関数staticmethodを内蔵しています.クラスの関数を静的な方法として定義します.
クラスの方法はクラスに使うもので、クラス自体はパラメータとしてクラスの方法に伝えられます.pythonは関数classmethodを内蔵してクラスの関数をクラスに定義します.
1.1、使用説明:
一般的には、ある種類の方法を使うには、まずオブジェクトを具体化してから呼び出す方法が必要です.@staticmethodまたは@classimethodを使うと、実例化を必要とせず、直接にクラス名で呼び出すことができます.
使用:直接類名.方法名() を選択します
1.2、違い:
@staticmethodは、自身のオブジェクトのselfと自身のクラスのclsパラメータを表示する必要はなく、使用関数と同じです.
使用:直接クラス名.属性名 または 直接类名.方法名.直接類名も直接類名()
@classimethodもselfパラメータは必要ありませんが、最初のパラメータは自分のクラスを表すclsパラメータが必要です.
使用:直接クラス名.属性名または 直接クラス名.方法名.
注:両者が定義している装飾器の呼び出し方法は同じです.@staticmethodでこのクラスに呼び出される属性のいくつかの方法は、直接類名.属性名または類名.方法名のみです.また、@classimethodはclsパラメータを持っているので、クラスの属性、クラスの方法、実装オブジェクトなどを呼び出すことができます.
二、静的方法(staticmethod)
通常、クラスで定義されている関数はすべて対象のバインディング方法であり、オブジェクトはバインディング方法を呼び出すと自動的にパラメータとして自分を渡します.この他にも、2つの一般的な方法があります.静的な方法と種類の方法、2つはクラスのためにカスタマイズされていますが、実例は必ず使用しなければなりません.誤報もありません.
は、クラス定義の名前空間に位置しています.具体的なタイプは動作しません.pythonは関数staticmethodを内蔵しています.クラスの関数を静的な方法として定義します.
class Foo:
def spam(x,y,z): # , ,self x
print(x,y,z)
spam=staticmethod(spam) # spam
前に習った装飾器の知識に基づいて、@staticmethodはspam=staticmethodと同じです.class Foo:
@staticmethod #
def spam(x,y,z):
print(x,y,z)
プレゼンテーションを使う:print(type(Foo.spam)) #
Foo.spam(1,2,3) #
f1=Foo()
f1.spam(3,3,3) # , ,
'''
2 3
3 3
'''
应用シーン:クラスを作るには様々な方法で実例を作成する必要がありますが、私たちは一つしかないです.init_関数の場合、静的な方法が役に立ちます.class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
@staticmethod
def now(): # Date.now() ,
t=time.localtime() #
return Date(t.tm_year,t.tm_mon,t.tm_mday) #
@staticmethod
def tomorrow():# Date.tomorrow() ,
t=time.localtime(time.time()+86400)
return Date(t.tm_year,t.tm_mon,t.tm_mday)
a=Date('1987',11,27) #
b=Date.now() #
c=Date.tomorrow() #
print(a.year,a.month,a.day)
print(b.year,b.month,b.day)
print(c.year,c.month,c.day)
三、類の方法(classmethod) クラスの方法はクラスに使うもので、クラス自体はパラメータとしてクラスの方法に伝えられます.pythonは関数classmethodを内蔵してクラスの関数をクラスに定義します.
class A:
x=1
@classmethod
def test(cls):
print(cls,cls.x)
class B(A):
x=2
B.test()
'''
:
2
'''
アプリケーションシーン:import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
@staticmethod
def now():
t=time.localtime()
return Date(t.tm_year,t.tm_mon,t.tm_mday)
class EuroDate(Date):
def __str__(self):
return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)
e=EuroDate.now()
print(e) # EuroDate.__str__,
'''
:
<__main__.date object="" at="">
'''
eはDate類で作られているので、EuroDate.u.u.を触発することはありません.stru解決方法はclassmethodを使うことです.import time
class Date:
def __init__(self,year,month,day):
self.year=year
self.month=month
self.day=day
# @staticmethod
# def now():
# t=time.localtime()
# return Date(t.tm_year,t.tm_mon,t.tm_mday)
@classmethod #
def now(cls):
t=time.localtime()
return cls(t.tm_year,t.tm_mon,t.tm_mday) # , cls
class EuroDate(Date):
def __str__(self):
return 'year:%s month:%s day:%s' %(self.year,self.month,self.day)
e=EuroDate.now()
print(e) # EuroDate.__str__, e EuroDate ,
'''
:
year:2017 month:3 day:3
'''
注意:静的な方法と種類の方法はクラスのために用意されていますが、例を挙げて使ってもいいです.例を挙げて呼び出した場合は混同されやすく、何をしたいのか分かりません.x=e.now() # e ,
print(x)
'''
:
year:2017 month:3 day:3
'''
四、知識をつける.strの使い方 # __str__ , ,
# ? ,
class People:
def __init__(self,name,age):
self.name=name
self.age=age
def __str__(self):
return '' %(self.name,self.age)
p1=People('egon',18)
print(p1)
str(p1) #----->p1.__str__()