草庐IT

self-documenting

全部标签

python - 在函数属性中访问 self

我正在尝试添加一个装饰器,该装饰器将可调用属性添加到函数,这些函数返回的对象与函数的返回值略有不同,但会在某个时候执行该函数。我遇到的问题是,当函数对象被传递到装饰器时,它是未绑定(bind)的并且不包含隐式的self参数。当我调用创建的属性函数(即string())时,我无法访问self,也无法将其传递给原始函数。defdeco(func):"""Addanattributetothefunctiontakesthesameargumentsasthefunctionbutmodifiestheoutput."""defstring(*args,**kwargs):returnstr

python - self 加入 Pandas

我想对Pandas数据框执行自连接,以便将某些行附加到原始行。每行都有一个标记“i”,指示应在右侧附加哪一行。d=pd.DataFrame(['A','B','C'],columns=['some_col'])d['i']=[2,1,1]In[17]:dOut[17]:some_coli0A21B12C1期望的输出:some_colisome_col_y0A2C1B1B2C1B也就是说,第2行附加到第0行,第1行附加到第1行,第1行附加到第2行(如i所示)。我的想法是pd.merge(d,d,left_index=True,right_on='i',how='left')但它会产生完全

python - 没有 self 的内部类功能

大家平安!我正在使用Python3.6.3,我发现奇怪的是这种构造是可能的:classTestClass(object):def__init__(self):self.arg="arg"deftest():print("Heytest")并使用:>>>TestClass.test()"Heytest"我知道在Python中有以self为参数的标准方法(不知道如何正确调用它们)、静态方法、类方法、抽象方法。但是test()是个什么样的方法呢?是静态方法吗?编辑:这种确定类内部函数的方法是否有任何有用的用例? 最佳答案 在python3

python - LXML 和 XSL document() 函数

您好,我有以下文件:合并.py:fromlxmlimportetreexml_input=etree.XML(open('a.xml','r').read())xslt_root=etree.XML(open('merge.xsl','r').read())transform=etree.XSLT(xslt_root)printstr(transform(xml_input))合并.xsl:a.xml:b.xml:这4个文件在同一目录中,当我调用merge.py时出现错误:lxml.etree.XSLTApplyError:CannotresolveURIstring://__STRI

python 萨克斯错误 "junk after document element"

我使用pythonsax来解析xml文件。xml文件其实就是多个xml文件的组合。看起来像这样:我的python代码如下。它显示“文档元素后的垃圾”错误。解决这个问题的任何好主意。谢谢。fromxml.sax.handlerimportContentHandlerfromxml.saximportmake_parser,SAXExceptionimportsysclassPostHandler(ContentHandler):def__init__(self):self.find=0self.buffer=''self.mapping={}defstartElement(self,na

python - 我应该使用 "self"来定义不需要从外部访问的类实例化变量/对象吗?

我不是一个完全的初学者,但对Python还是个新手。今天在做一个项目时,我有一个想法,想知道“self”的用法;关于我过去一段时间一直在阅读的内容,我仍然无法弄清楚它是否总是必要的。我的问题仅涉及类实例和实例参数/变量。这个问题不是关于影响所有实例的类变量。示例:classC:def__init__(self,parent=None):super(C,self).__init__(parent)self.some_temp_var=AnotherClass()self.important_property=self.some_temp_var.bring_the_jewels()ins

python - 使用 __getattr__(self, name) 访问实例的其他属性

在Python的documentation,onthe__getattr__function中它说:Notethatiftheattributeisfoundthroughthenormalmechanism,__getattr__()isnotcalled.(Thisisanintentionalasymmetrybetween__getattr__()and__setattr__().)Thisisdonebothforefficiencyreasonsandbecauseotherwise__getattr__()wouldhavenowaytoaccessotherattrib

python - 在 Nose 测试课上使用 __init__(self) 而不是 setup(self) 有缺点吗?

为运行nosetests-sclassTestTemp():def__init__(self):print'__init__'self.even=0defsetup(self):print'__setup__'self.odd=1deftest_even(self):print'test_even'even_number=10asserteven_number%2==self.evendeftest_odd(self):print'test_odd'odd_number=11assertodd_number%2==self.odd打印出以下内容。__init____init____se

python - 为什么 `class X: mypow = pow` 有效? `self` 呢?

这有效并愉快地打印81:classX:mypow=powprint(X().mypow(3,4))但是为什么?方法不是给出了额外的“self”参数并且应该完全混淆吗?为了对比,我也用自己的Pow函数试了一下:defPow(x,y,z=None):returnx**yclassY:myPow=Powprint(Pow(3,4))print(Y().myPow(3,4))直接函数调用打印81并且方法调用按预期崩溃,因为它确实获得了额外的实例参数:Python3:TypeError:unsupportedoperandtype(s)for**orpow():'Y'and'int'Python

python - 我怎样才能停用 'Warning: Source ID 510 was not found when attempting to remove it - GLib.source_remove(self._idle_event_id)' ?

当我执行#!/usr/bin/envpythonimportmatplotlib.pyplotaspltplt.plot([1,2,3,4])plt.show()(和更复杂的例子)我明白了/usr/local/lib/python3.4/dist-packages/matplotlib/backends/backend_gtk3.py:215:Warning:SourceID7wasnotfoundwhenattemptingtoremoveitGLib.source_remove(self._idle_event_id)是什么原因导致的?我该如何消除这些警告?我知道我可以用impor