首页 > 解决方案 > How does assigning methods to each other work? ( " __call__ = __getattr__ " )

问题描述

I only knew about assigning values to variables etc. But in this case, assigning a method to another method? How does that even work?

What the code lines below do? Is there a name for this kind of usage.

__repr__ = __str__
__call__ = __getattr__

Example 1

class Chain(object):
  def __init__(self,path=''):
    print("Constructor.")
    self.__path=path
  def __getattr__(self,path):
    print("getattr method called. path: %s" % path)
    return Chain(self.__path+'/'+path)
  def __str__(self):
    print("String metod called %s" % self.__path)
    return self.__path
  # This part runs before init!
  print("Weird statements.")
  __repr__=__str__
  __call__=__getattr__


if __name__ == '__main__':
    c = Chain()
    ## Print function calls __call__ and __getattr__ methods, first getattr for 'users' attribute, then 'calls' for 'michael' string argument.
    print(c.users("Michael"))

Output:

Weird statements.
Constructor.
getattr method called. path: users
Constructor.
getattr method called. path: Michael
Constructor.
String metod called /users/Michael
/users/Michael

Thank you.

标签: python

解决方案


下面的代码行是做什么的?这种用法有名字吗。

没有名字,因为它没有什么特别之处。它只是将一个变量分配给另一个变量。__repr__ = __str__与 完全相同b = a,它只是为同一个对象创建第二个引用(使用不同的名称)。

“神奇”在于 Python 的类主体基本上充当了一个函数,它是按顺序执行的代码,最后收集所有“局部变量”并传递给类构造函数(不要与类的构造函数混淆)成为类的属性,这就是 Python 中的方法。但在最后一步之前,它只是被执行的代码。

__str__TBH 作为委托人,这个特定的分配也是无用的(或者更确切地说是错误的方式) ,所以如果你希望两者都是一样的__repr__,你可以重写。__repr__

除此之外:

self.__path=path

这是非常糟糕的风格,你不应该使用它。Python 中的名称修改存在与继承相关的原因,它们与“信息隐藏”无关,也不是创建“私有成员”的一种方式。如果要指定成员是“私有的”,则约定是使用单个下划线前缀。


推荐阅读