Class method differences in Python: bound, unbound and static

后端 未结 13 1059
日久生厌
日久生厌 2020-11-22 08:54

What is the difference between the following class methods?

Is it that one is static and the other is not?

class Test(object):
  def method_one(self)         


        
相关标签:
13条回答
  • 2020-11-22 09:46

    method_two won't work because you're defining a member function but not telling it what the function is a member of. If you execute the last line you'll get:

    >>> a_test.method_two()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: method_two() takes no arguments (1 given)
    

    If you're defining member functions for a class the first argument must always be 'self'.

    0 讨论(0)
提交回复
热议问题