, Python,
self.func
,
, , . , , ( ) , : . , , .
, func self, , , , .
,
print type(time.gmtime)
class Foo(object):
func = time.gmtime
def go(self):
print type(self.func)
return self.func(0.0)
go Foo().go(),
<type 'builtin_function_or_method'>
<type 'builtin_function_or_method'>
. time.gmtime ( ), . ,
def tmp(stamp):
return time.gmtime(stamp)
print type(tmp), tmp
class Foo(object):
func = tmp
def go(self):
print type(self.func), self.func
return self.func(0.0)
<type 'function'> <function tmp at 0x7f34d9983578>
<type 'instancemethod'> <bound method Foo.tmp of <__main__.Foo object at ...>>
tmp , , , Object Foo . , tmp Foo instancemethod. go Foo().go(), tmp,
Foo.func(self, 0.0)
tmp(self, 0.0)
:
TypeError: tmp() takes exactly 1 argument (2 given)
- class/instance ?
1:
Python,
, , , ; , .
, , , . , , .
, ,
import time
def tmp(stamp):
return time.gmtime(stamp)
class Foo(object):
def __init__(self):
self.func = tmp
def go(self):
return self.func(0.0)
print time.strftime('%Y', Foo().go())
self.func tmp(0.0), .
2:
staticmethod ,
class Foo(object):
func = staticmethod(tmp)
def go(self):
return self.func(0.0)
self.func tmp.
class Foo(object):
@staticmethod
def func(stamp):
return time.gmtime(stamp)
def go(self):
return self.func(0.0)