Python-为什么我可以使用实例调用类方法? [英] Python - why can I call a class method with an instance?

查看:212
本文介绍了Python-为什么我可以使用实例调用类方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Python的新手,并且已经读了一些书,我在自定义类类方法中创建了一些方法,而不是实例方法.

所以我测试了我的代码,但是我没有更改某些方法调用以在类而不是实例中调用该方法,但是它们仍然有效:

class myClass:
   @classmethod:
   def foo(cls):
      print 'Class method foo called with %s.'%(cls)

   def bar(self):
      print 'Instance method bar called with %s.'%(self)

myClass.foo()
thing = myClass()
thing.foo()
thing.bar()

这将产生:

class method foo called with __main__.myClass.
class method foo called with __main__.myClass.
instance method bar called with <__main__.myClass instance at 0x389ba4>.

所以我想知道的是为什么我可以在实例(thing.foo)上调用类方法(foo),(尽管它是传递给该方法的类)?有点道理,因为事物"是"myClass",但是我期望Python给出一个错误,说类似"foo是类方法,不能在实例上调用".

这仅仅是继承权吗?thing对象从其超类继承foo方法吗?

如果我尝试通过类调用实例方法:

myClass.bar()

然后我得到:

TypeError: unbound method bar() must be called with myClass instance...

这很有意义.

解决方案

您可以在实例上调用它,因为@classmethod文档的一些相关信息

可以在类(例如C.f())或实例上调用它 (例如C().f()).该实例除其类外均被忽略.如果一个 类方法是为派生类调用的,派生类对象 作为隐含的第一个参数传递.

@classmethod 此处上,也进行了很好的SO讨论.

New to Python and having done some reading, I'm making some methods in my custom class class methods rather than instance methods.

So I tested my code but I hadn't changed some of the method calls to call the method in the class rather than the instance, but they still worked:

class myClass:
   @classmethod:
   def foo(cls):
      print 'Class method foo called with %s.'%(cls)

   def bar(self):
      print 'Instance method bar called with %s.'%(self)

myClass.foo()
thing = myClass()
thing.foo()
thing.bar()

This produces:

class method foo called with __main__.myClass.
class method foo called with __main__.myClass.
instance method bar called with <__main__.myClass instance at 0x389ba4>.

So what I'm wondering is why I can call a class method (foo) on an instance (thing.foo), (although it's the class that gets passed to the method)? It kind of makes sense, as 'thing' is a 'myClass', but I was expecting Python to give an error saying something along the lines of 'foo is a class method and can't be called on an instance'.

Is this just an expected consequence of inheritance with the 'thing' object inheriting the foo method from its superclass?

If I try to call the instance method via the class:

myClass.bar()

then I get:

TypeError: unbound method bar() must be called with myClass instance...

which makes perfect sense.

解决方案

You can call it on an instance because @classmethod is a decorator (it takes a function as an argument and returns a new function).

Here is some relavent information from the Python documentation

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

There's also quite a good SO discussion on @classmethod here.

这篇关于Python-为什么我可以使用实例调用类方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆