python—如何加载重载方法
问题: python是否按这种方式加载方法-谁最后一个,谁赢?即使您有两个方法共享确切的名称,即使具有不同的参数不同的签名,最后一个方法也会在不给出运行时错误的情况下否决前面的所有方法? 如果python没有重载,那么python推荐使用什么方法来像JAVA一样进行重载? 下面是一个例子:python—如何加载重载方法,python,constructor,Python,Constructor,问题: python是否按这种方式加载方法-谁最后一个,谁赢?即使您有两个方法共享确切的名称,即使具有不同的参数不同的签名,最后一个方法也会在不给出运行时错误的情况下否决前面的所有方法? 如果python没有重载,那么python推荐使用什么方法来像JAVA一样进行重载? 下面是一个例子: class Base(object): def __init__(self): print "Base created without args" def __init__(s
class Base(object):
def __init__(self):
print "Base created without args"
def __init__(self, a):
print "Base created " + a + "\n"
print Basetest为我提供了:
Base created test
<__main__.Base object at 0x1090fff10>
Traceback (most recent call last):
File "${path to super file}/super.py", line 27, in <module>
print Base()
TypeError: __init__() takes exactly 2 arguments (1 given)
基本上,你自己已经回答了这个问题。Python不关心方法签名,只关心名称的重要性。这也适用于模块级功能。
与Java不同,Python允许您为方法参数指定默认值,我认为这更方便:
class Base(object):
def __init__(self, a=None):
if a is None:
print "Base created without args."
else:
print "Base created with %s" % a
a = Base() # prints "Base created without args."
b = Base(123) # prints "Base created with 123."
基本上,你自己已经回答了这个问题。Python不关心方法签名,只关心名称的重要性。这也适用于模块级功能。
与Java不同,Python允许您为方法参数指定默认值,我认为这更方便:
class Base(object):
def __init__(self, a=None):
if a is None:
print "Base created without args."
else:
print "Base created with %s" % a
a = Base() # prints "Base created without args."
b = Base(123) # prints "Base created with 123."
您可以使用装饰器滚动自己的方法重载程序:
class OverloadFunction(object):
def __new__(cls, f):
self = object.__new__(cls)
setattr(self, "_dct", {})
return self.overload(())(f)
def overload(self, signature):
def wrapper(f):
self._dct[signature] = f
return self
return wrapper
def __call__(self, *args, **kwargs):
return self._dct[self._get_signature(args)](*args, **kwargs)
def _get_signature(self, obj):
return tuple(type(x) for x in obj)
@OverloadFunction
def hello():
print "hello, no args"
@hello.overload((int,))
def hello(i):
print "hello with an int argument:", i
@OverloadFunction
def add(): pass
@add.overload((int, int))
def add(a, b):
print "integer addition, %d + %d = %d" % (a, b, a + b)
@add.overload((str, int))
def add(a, b):
print "string concatentation, %r + %d = %r" % (a, b, a + str(b))
hello()
hello(1)
add(2, 3)
add("a", 3)
哪些产出:
hello, no args
hello with an int argument: 1
integer addition, 2 + 3 = 5
string concatentation, 'a' + 3 = 'a3'
您可以使用装饰器滚动自己的方法重载程序:
class OverloadFunction(object):
def __new__(cls, f):
self = object.__new__(cls)
setattr(self, "_dct", {})
return self.overload(())(f)
def overload(self, signature):
def wrapper(f):
self._dct[signature] = f
return self
return wrapper
def __call__(self, *args, **kwargs):
return self._dct[self._get_signature(args)](*args, **kwargs)
def _get_signature(self, obj):
return tuple(type(x) for x in obj)
@OverloadFunction
def hello():
print "hello, no args"
@hello.overload((int,))
def hello(i):
print "hello with an int argument:", i
@OverloadFunction
def add(): pass
@add.overload((int, int))
def add(a, b):
print "integer addition, %d + %d = %d" % (a, b, a + b)
@add.overload((str, int))
def add(a, b):
print "string concatentation, %r + %d = %r" % (a, b, a + str(b))
hello()
hello(1)
add(2, 3)
add("a", 3)
哪些产出:
hello, no args
hello with an int argument: 1
integer addition, 2 + 3 = 5
string concatentation, 'a' + 3 = 'a3'
可能的重复可能的重复另外,duck类型消除了大多数其他场景中方法重载的需要。此外,duck类型消除了大多数其他场景中方法重载的需要。尽管,as,您可能应该只使用默认参数。尽管,as,您可能应该改用默认参数。