class Base(object):
@classmethod
def new(cls):
print("Base.new(%r)" % (cls,))
return cls()
class UseSuper(Base):
@classmethod
def new(cls):
print("UseSuper.new(%r)" % (cls,))
return super(UseSuper, cls).new() # passes cls as the first arg
class NoSuper(Base):
@classmethod
def new(cls):
print("NoSuper.new(%r)" % (cls,))
return Base.new() # passes Base as the first arg
class UseFunc(Base):
@classmethod
def new(cls):
print("UseFunc.new(%r)" % (cls,))
return Base.new.im_func(cls) # or `.__func__(cls)`. # passes cls as the first arg
print(UseSuper.new())
print('-'*60)
print(NoSuper.new())
print('-'*60)
print(UseFunc.new())
Y2xhc3MgQmFzZShvYmplY3QpOgogICAgQGNsYXNzbWV0aG9kCiAgICBkZWYgbmV3KGNscyk6CiAgICAgICAgcHJpbnQoIkJhc2UubmV3KCVyKSIgJSAoY2xzLCkpCiAgICAgICAgcmV0dXJuIGNscygpCgpjbGFzcyBVc2VTdXBlcihCYXNlKToKICAgIEBjbGFzc21ldGhvZAogICAgZGVmIG5ldyhjbHMpOgogICAgICAgIHByaW50KCJVc2VTdXBlci5uZXcoJXIpIiAlIChjbHMsKSkKICAgICAgICByZXR1cm4gc3VwZXIoVXNlU3VwZXIsIGNscykubmV3KCkgIyBwYXNzZXMgY2xzIGFzIHRoZSBmaXJzdCBhcmcKCmNsYXNzIE5vU3VwZXIoQmFzZSk6CiAgICBAY2xhc3NtZXRob2QKICAgIGRlZiBuZXcoY2xzKToKICAgICAgICBwcmludCgiTm9TdXBlci5uZXcoJXIpIiAlIChjbHMsKSkKICAgICAgICByZXR1cm4gQmFzZS5uZXcoKSAgIyBwYXNzZXMgQmFzZSBhcyB0aGUgZmlyc3QgYXJnCgpjbGFzcyBVc2VGdW5jKEJhc2UpOgogICAgQGNsYXNzbWV0aG9kCiAgICBkZWYgbmV3KGNscyk6CiAgICAgICAgcHJpbnQoIlVzZUZ1bmMubmV3KCVyKSIgJSAoY2xzLCkpCiAgICAgICAgcmV0dXJuIEJhc2UubmV3LmltX2Z1bmMoY2xzKSAgIyBvciBgLl9fZnVuY19fKGNscylgLiAjIHBhc3NlcyBjbHMgYXMgdGhlIGZpcnN0IGFyZwoKcHJpbnQoVXNlU3VwZXIubmV3KCkpCnByaW50KCctJyo2MCkKcHJpbnQoTm9TdXBlci5uZXcoKSkKcHJpbnQoJy0nKjYwKQpwcmludChVc2VGdW5jLm5ldygpKQ==