'''
Conflicting method names in python
'''
class ConflictCheck(type):
def __new__(meta, name, bases, dct):
# determine attributes per base class, except for magic ones
attrs_per_base = [set(a for a in dir(b) if not a.startswith("__"))
for b in bases]
if len(set.union(*attrs_per_base)) < sum(map(len, attrs_per_base)):
raise ValueError("attribute conflict")
return super(ConflictCheck, meta).__new__(meta, name, bases, dct)
class Tiger():
@staticmethod
def speak():
print "Rawr!";
class Lion():
@staticmethod
def speak():
print "Roar!";
class Liger(Lion, Tiger):
__metaclass__ = ConflictCheck # will raise an error at definition time
Liger.speak(); ''' this prints "Rawr" instead of printing an error message. '''
'''Is there any way to detect method name collisions like this one?'''
JycnCkNvbmZsaWN0aW5nIG1ldGhvZCBuYW1lcyBpbiBweXRob24KJycnCgpjbGFzcyBDb25mbGljdENoZWNrKHR5cGUpOgogICAgZGVmIF9fbmV3X18obWV0YSwgbmFtZSwgYmFzZXMsIGRjdCk6CiAgICAgICAgIyBkZXRlcm1pbmUgYXR0cmlidXRlcyBwZXIgYmFzZSBjbGFzcywgZXhjZXB0IGZvciBtYWdpYyBvbmVzCiAgICAgICAgYXR0cnNfcGVyX2Jhc2UgPSBbc2V0KGEgZm9yIGEgaW4gZGlyKGIpIGlmIG5vdCBhLnN0YXJ0c3dpdGgoIl9fIikpCiAgICAgICAgICAgICAgICAgICAgICAgICAgZm9yIGIgaW4gYmFzZXNdCiAgICAgICAgaWYgbGVuKHNldC51bmlvbigqYXR0cnNfcGVyX2Jhc2UpKSA8IHN1bShtYXAobGVuLCBhdHRyc19wZXJfYmFzZSkpOgogICAgICAgICAgICByYWlzZSBWYWx1ZUVycm9yKCJhdHRyaWJ1dGUgY29uZmxpY3QiKQogICAgICAgIHJldHVybiBzdXBlcihDb25mbGljdENoZWNrLCBtZXRhKS5fX25ld19fKG1ldGEsIG5hbWUsIGJhc2VzLCBkY3QpCgpjbGFzcyBUaWdlcigpOgogICAgQHN0YXRpY21ldGhvZAogICAgZGVmIHNwZWFrKCk6CiAgICAgICAgcHJpbnQg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