简体   繁体   English

[Python]调用方法(个体经营)

[英][Python]Calling method(self)

I can not understand following commented line. 我不明白以下评论。 It's a python program. 这是一个python程序。


class B:
 def bbb(self):
    method = self.commands[0]
    method(self) #I can't umderstand this line

class A(B):
    def aaa(self):
        print 'aaa was called'
    commands = [aaa]

c = A()
c.bbb()

Output: aaa was called 输出:aaa被称为


I think above aaa method takes no arguments. 我认为上述aaa方法不需要任何参数。 But to run this code, I need to pass "self" to aaa argument. 但是要运行此代码,我需要将“ self”传递给aaa参数。 Why? 为什么? Are there any docs explaining this?What category this problem belongs? 是否有任何文档对此进行解释?此问题属于什么类别?

Any simple codes are very welcome. 任何简单的代码都非常受欢迎。 Because my English skill is too low. 因为我的英语水平太低了。 So improving this question is welcome too. 因此,也欢迎改进此问题。

I encountered this problem, when I was reading cpython/Lib/distutils/cmd.py:Command.get_sub_commands(). 我在阅读cpython / Lib / distutils / cmd.py:Command.get_sub_commands()时遇到了这个问题。

Thank you for reading. 感谢您的阅读。

Wow, this is confusingly written. 哇,这是令人困惑的文字。 Working backward from the code itself: 从代码本身向后工作:

c = A()

Creates an instance of A. Looking at A: 创建A的实例。查看A:

def aaa(self):
    print 'aaa was called'
commands = [aaa]

This is a bit confusingly written; 这有点令人困惑。 it makes more sense like this: 这样更有意义:

def aaa(self):
    print 'aaa was called'

commands = [aaa]

Defines a method aaa , then a class variable commands which contains aaa as an element. 定义方法aaa ,然后定义一个类变量 commands ,其中包含aaa作为元素。 Now, looking at the next line of the program: 现在,看程序的下一行:

c.bbb()

Since A has no bbb and A inherits from B , we consult B : 由于A没有bbb并且AB继承,所以我们咨询B

class B:
 def bbb(self):
    method = self.commands[0]
    method(self)

Since we've established that commands is [aaa] , the first line means method = aaa . 由于我们已经确定commands[aaa] ,因此第一行表示method = aaa So the second line is effectively aaa(self) . 因此,第二行实际上是aaa(self)

This line: 这行:

method(self) #I can't umderstand this line

Calls the function aaa() . 调用函数aaa() In your function declaration: 在函数声明中:

def aaa(self):

aaa does takes an argument ( self ). aaa确实接受了一个参数( self )。 That's why you have to call it with method(self) . 这就是为什么您必须使用method(self)调用它的原因。

Since self.commands[0] is a function, calling method(self) is equal to: 由于self.commands[0]是一个函数,因此调用method(self)等于:

aaa(self)

Comment if you have something else to ask! 评论您还有其他问题要问!

The way the code sample is done makes it a little harder to tell what's going on. 代码示例的完成方式使它很难说出正在发生的事情。 However, it's equivalent to: 但是,它等效于:

child_methods = [] # a list of all the methods in `Child`

class Parent(object):
    def parent_method(self):
        print "parent_method() called"
        method = child_methods[0]
        method(self)

class Child(Parent):
    def child_method(self):
        print "child_method() called"

# add child_method to child_methods
child_methods.append(Child.child_method)

As you can see, child_methods[0] will actually be the function Child.child_method , which is a plain function, and not a bound method. 如您所见, child_methods[0]实际上将是Child.child_method函数,这是一个普通函数,而不是绑定方法。 It's not associated with an instance of Child , which is why you can and have to pass in the self yourself. 它与Child实例无关,这就是为什么您可以并且必须自己传递self You'd get a bound method from a Child instance with: 您将从带有以下内容的Child实例中获得绑定方法:

child_obj = Child()
bound_child_method = child_obj.child_method

This is made unclear by the fact Python will look up attributes in the type of an object if they're not found in the instance. 如果在实例中找不到属性,Python会查找对象类型的属性,这一点并不清楚。 For example: 例如:

# A dummy class to hold attributes
class Foo(object):
    pass

Foo.bar = 123 # we're adding an attribute to the type itself

foo1 = Foo()
print foo1.bar # prints 123
foo1.bar = 456 # this `bar` attribute "hides" the one on the type
print foo1.bar # prints 456
foo2 = Foo()
print foo2.bar # prints the 123 from the type again

This is why, in your code sample, commands is really a "global" variable, it just gets accessed confusingly through an instance of B . 这就是为什么在您的代码示例中, commands实际上是一个“全局”变量,它只是通过B的实例引起混乱地访问。 (This is not necessarily a bad practice if only objects of B or its children access this variable, but the lookup rules are a minor gotcha.) (如果仅B对象或其子对象访问此变量,这不一定是不好的做法,但是查找规则是次要的陷阱。)

By the way it is better to use new-style classes, class A(object):... 顺便说一句,最好使用新型类,即类A(object):...

All methods of class in python have self as first argument, except of class methods. python中所有class方法都将self作为第一个参数,除了class方法。 Here is example about self: 这是关于自我的例子:

def x(first, arg):
    print "Called x with arg=",arg
    print first

class A(object):
     some_method = x

a = A()
a.some_method("s")`

http://docs.python.org/2/tutorial/classes.html#random-remarks http://docs.python.org/2/tutorial/classes.html#random-remarks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM