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__ Resul

继承自python中的xmlrpclib.ServerProxy

为什么这段代码不起作用? #!/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__ 结果: F

creating Python classes with arbitrarily substituted attribute name

I apologize for not giving this question a better title; the reason that I am posting it is that I don't even have the correct terminology to know what I am looking for. I have defined a class with an attribute 'spam': def SpamClass(object): def __init__(self, arg): self.spam = arg def __str__(self): return self.spam I want to create a (sub/sibling?)class

用任意替换的属性名创建Python类

我很抱歉不给这个问题一个更好的标题; 我发布它的原因是我甚至没有正确的术语来知道我在找什么。 我定义了一个具有“垃圾邮件”属性的类: def SpamClass(object): def __init__(self, arg): self.spam = arg def __str__(self): return self.spam 我想创建一个(sub / sibling?)类,它具有完全相同的功能,但具有名为'eggs'而不是'spam'的属性: def EggsClass(object): de

Checking whether a variable is an integer or not

我如何检查一个变量是否是一个整数? If you need to do this, do isinstance(<var>, int) unless you are in Python 2.x in which case you want isinstance(<var>, (int, long)) Do not use type . It is almost never the right answer in Python, since it blocks all the flexibility of polymorphism. For instance, if you subclass int , your new class should register as an int , which type will

检查一个变量是否是一个整数

我如何检查一个变量是否是一个整数? 如果你需要这样做,那么 isinstance(<var>, int) 除非你在Python 2.x中,在这种情况下你需要 isinstance(<var>, (int, long)) 不要使用type 。 它几乎从来都不是Python中的正确答案,因为它阻止了多态的所有灵活性。 例如,如果你继承了int ,你的新类应该注册为一个int type ,这种type不会这样做: class Spam(int): pass x = Spam(0) type(x) == int # False isinstanc

Modifying a Python class

I'd like to modify all classes in Python. For example str and int and others like Person(object). I'd like to add an attribute to them and to change the way its methods works. Which is the best approach for this? Metaclasses? While you can do this for classes defined in python code (it will not work for builtin ones) by reassigning their attributes please do not actually do so. J

修改一个Python类

我想修改Python中的所有类。 例如str和int以及像Person(object)这样的其他类。 我想为它们添加一个属性并改变它的方法的工作方式。 这是最好的方法? 元类? 虽然你可以通过重新分配它们的属性来为python代码中定义的类执行此操作(它不适用于内置代码),但实际上并不这样做。 只需子类并使用子类,或者编写以该类的实例作为参数而不是添加自己的方法的函数。 做你必须介意的事情会导致尴尬,脆弱的代码,特别是如

Variable value is determined by function call

This is probably weird but I would like to declare a variable without a fixed value, but "linked" in some way to the result of a function. The goal is for the end user to manipulate a variable, but each time the variable's value is used, its value may change. This is the current result I get: from random import randint def randomfun(): return randint(1, 100) an_int = rando

变量值由函数调用确定

这可能很奇怪,但我想声明一个没有固定值的变量,但是以某种方式“链接”到函数的结果。 目标是让最终用户操纵变量,但每次使用变量的值时,其值可能会改变。 这是我得到的最新结果: from random import randint def randomfun(): return randint(1, 100) an_int = randomfun print an_int # Print the function object print an_int() # Print the result of randomfun() 我想得到的是print an_int实际调用random

Intersphinx links to python 2 docs even with python 3 in intersphinx

I am building docs with sphinx (Sphinx==1.7.0) using the intersphinx extension. I noticed that it was linking to python 2 docs by default, but I preferred linking to python 3 docs, so I changed the intersphinx_mapping in my conf.py to the following: # conf.py ... intersphinx_mapping = { 'python': ('https://docs.python.org/3/': None), } ... However, when I make references in my source files

Intersphinx甚至可以在intersphinx中使用python 3链接到python 2文档

我正在用狮身人面像(Sphinx == 1.7.0)使用intersphinx扩展名构建文档。 我注意到它默认链接到python 2文档,但我更喜欢链接到python 3文档,所以我将intersphinx_mapping中的intersphinx_mapping改为以下内容: # conf.py ... intersphinx_mapping = { 'python': ('https://docs.python.org/3/': None), } ... 但是,当我在我的源文件中引用python对象时,我发现它们仍然默认连接到python 2.如果我在前缀str前面以便它

Include my markdown README into Sphinx

I would like to include my project's README.md into my Sphinx documentation like in Can sphinx link to documents that are not located in directories below the root document? - which is that in the resulting Sphinx html documentation I click on a link in the table of contents on the welcome page and get to the README.md . For that a document readme_link.rst is created which contains the lin

将我的降价自述文件包含在狮身人面像中

我想将我的项目的README.md到我的Sphinx文档中,如Can sphinx链接到不在根文档下的目录中的文档? - 在Sphinx html文档中,我点击欢迎页面上目录中的链接,进入README.md 。 为此,将创建一个包含行的文档readme_link.rst Readme File ----------- .. include:: ../../README.md 并添加该行 README <readme_link> 进入index.rst的index.rst 。 README.md ,我的README.md不会被解析为Markdown,而只是按原样打印

Refer reST label in python docstring

I want to refer to reST label in my python method docstring and am expecting Sphinx to generate appropriate links. I tried the following which does not work for me. myown.rst .. my-label: Some explanation ... mymodule.py def somefunc(): """ See :ref:`my-label`. """ ... rest of the code ... This produces "See my-label" as text in the generated html file while I am expe

在python docstring中引用reST标签

我想在我的Python方法docstring中引用reST标签,并希望Sphinx生成适当的链接。 我尝试了以下这对我不起作用。 myown.rst .. my-label: Some explanation ... mymodule.py def somefunc(): """ See :ref:`my-label`. """ ... rest of the code ... 这会在生成的html文件中生成“See my-label”作为文本,而我期待它创建适当的超链接。 我在python-docs中看到了这种引用(例如,参见https://docs.python.org/3/

sphinx not to include source code during build

I am trying to document my python packages using Sphinx 1.2.1. My definition of the rst file contains some description about each modules, usage and adding the autodoc syntax for restructured text as below. module ------ .. automodule:: RAT.REPORTER.bemrstcreator :members: :undoc-members: :show-inheritance: The above setup makes a clear html build for me without any problem. It

构建过程中,sphinx不包括源代码

我正在尝试使用Sphinx 1.2.1来记录我的Python包。 我对第一个文件的定义包含一些关于每个模块的描述,用法以及为重新构造的文本添加autodoc语法,如下所示。 module ------ .. automodule:: RAT.REPORTER.bemrstcreator :members: :undoc-members: :show-inheritance: 上面的设置为我创建了一个清晰的html版本,没有任何问题。 它从所有类,它的相关成员等中派生出文档,但是它包含了html中的源代码。 我如何

reference a function generated by autodoc in Sphinx?

I am using the Sphinx autodoc feature to generate documentation based on the docstrings of my Python library. The syntax for cross referencing is found here A label must precede the section in order to allow that section to be referenced from other areas of the documentation. What I have is a .rst (ReStructeredText) file for one of my classes. It uses .. autoclass:: classname :members

引用由狮身人面像中的autodoc生成的函数?

我使用Sphinx autodoc功能根据我的Python库的文档字符串生成文档。 交叉引用的语法在这里找到 标签必须位于该部分的前面,以便可以从该文档的其他区域引用该部分。 我拥有的是我的一个类的.rst(ReStructeredText)文件。 它用 .. autoclass:: classname :members: 为该课程生成文档。 我的问题是,我将如何从文档中的另一个.rst文档引用该类的自动生成方法? 如果我尝试在方法的文档字符串中放置一个标签,Sphi