inheritance from xmlrpclib.ServerProxy in python

Why this code doesn't work ?

#!/usr/bin/python2
from xmlrpclib import ServerProxy
class ServerProxy1(ServerProxy):
    def __str__(self):
        return str(self.__host)


proxy = ServerProxy1("http://workshop:58846/")
print proxy

Original_str_:

    def __repr__(self):
        return (
            "" %
            (self.__host, self.__handler)
            )

    __str__ = __repr__

Result:

  File "/usr/lib/python2.7/xmlrpclib.py", line 793, in close
    raise Fault(**self._stack[0])
xmlrpclib.Fault: :method "_ServerProxy1__host.__str__" is not supported'>

The answer is hiding in this SO post

The member self.__host in class ServerProxy was declared using the double-underscore naming that's meant to indicate that it shouldn't be accessed by derived classes. To do this, the interpreter mangles its name internally in the form _className__memberName -- Python isn't C++, and treats that 'private' notation as a strong hint, not as an absolute prohibition.

When code is written with the double underscore prefix, you can access it like

class ServerProxy1(ServerProxy):
    def __str__(self):
        return str(self._ServerProxy__host)

..but you shouldn't be surprised if a future version of the ServerProxy class changes its internal implementation and breaks your code.

链接地址: http://www.djcxy.com/p/75516.html

上一篇: 在CSS / DOM中,它是如何阻止显示:块的输入框扩展到其容器大小的

下一篇: 继承自python中的xmlrpclib.ServerProxy