草庐IT

assign_attributes

全部标签

python - [] = ()、() = () 和 {} = () 'assignments'

我惊讶地发现,在Python3中,前两个没有任何问题:>>>[]=()>>>()=()>>>{}=()File"",line1SyntaxError:can'tassigntoliteral在Python2.7中,只有第一个不会引发任何问题:>>>[]=()>>>()=()File"",line1SyntaxError:can'tassignto()>>>{}=()File"",line1SyntaxError:can'tassigntoliteral这里发生了什么?为什么其中任何一个都不会引发错误?为什么()=()可能被添加为在Python3中有效?*注意,您可以将右侧替换为任何空的

python - 属性错误 : 'module' object (scipy) has no attribute 'misc'

我从ubuntu12.04更新到ubuntu12.10并且我编写的python模块突然不再适用于模块scipy没有属性“misc”的错误消息。这以前有效。更新后我仍在使用python2.7。这是代码崩溃的地方importscipyscipy.misc.imsave(slice,dat)有什么想法吗? 最佳答案 >>>importscipy>>>scipy.miscTraceback(mostrecentcalllast):File"",line1,inAttributeError:'module'objecthasnoattribu

Python范围: "UnboundLocalError: local variable ' c' referenced before assignment"

这个问题在这里已经有了答案:UnboundLocalErroronlocalvariablewhenreassignedafterfirstuse(13个回答)Usingglobalvariablesinafunction(24个回答)关闭8年前。我正在努力解决这个问题:c=1deff(n):printc+ndefg(n):c=c+nf(1)#=>2g(1)#=>UnboundLocalError:localvariable'c'referencedbeforeassignment谢谢! 最佳答案 在函数中,分配给的变量默认被视为局

python - 从同名脚本导入已安装的包会引发 "AttributeError: module has no attribute"或 "ImportError: cannot import name"

我有一个名为requests.py的脚本,用于导入requests包。该脚本要么无法访问包中的属性,要么无法导入它们。为什么这不起作用,我该如何解决?以下代码引发AttributeError。importrequestsres=requests.get('http://www.google.ca')print(res)Traceback(mostrecentcalllast):File"/Users/me/dev/rough/requests.py",line1,inimportrequestsFile"/Users/me/dev/rough/requests.py",line3,in

python - 避免 Pylint 警告 E1101 : 'Instance of .. has no .. member' for class with dynamic attributes

想象一个函数,它动态地使用setattr向对象添加属性。这样做的原因是我想将一些外部结构(例如给定的参数树)映射到一个对象:my_object=SomeClass()apply_structure(my_object,some_descriptor)my_object.device1.enabled=True从技术上讲,这是可行的,但Pylint当然正确地提示'device1'不是SomeClass的成员。我可以禁用警告,但这会很糟糕(因为在所有情况下,当属性由于拼写错误等原因不存在时,我仍然希望得到警告)。是否有一种常见且合法的(Pylint-proof)方法可以动态地将成员添加到不

python - 属性错误 : module 'html.parser' has no attribute 'HTMLParseError'

这是提示,我该如何解决?我使用Python3.5.1通过virtualenv创建了一个虚拟环境源代码在我friend的电脑上运行良好错误:Traceback(mostrecentcalllast):File"manage.py",line10,inexecute_from_command_line(sys.argv)File"A:\Python3.5\lib\site-packages\django\core\management\__init__.py",line385,inexecute_from_command_lineutility.execute()File"A:\Pytho

python - graph.write_pdf ("iris.pdf") AttributeError : 'list' object has no attribute 'write_pdf'

我的代码是按照google的机器学习类的。两个代码是一样的。我不知道为什么会显示错误。可能是变量的类型是错误的。但是google的代码对我来说是一样的。谁有遇到过这个问题吗?这是错误[012][012]Traceback(mostrecentcalllast):File"/media/joyce/oreo/python/machine_learn/VisualizingADecisionTree.py",line34,ingraph.write_pdf("iris.pdf")AttributeError:'list'objecthasnoattribute'write_pdf'[Fin

python - 如何处理 Pylint 的 "too-many-instance-attributes"消息?

我刚刚尝试用Pylint对一些代码进行lint,最后剩下的错误是R0902:too-many-instance-attributes(8/7)我理解限制实例属性数量背后的基本原理,但七个似乎有点少。我也意识到linter不应该有最后的发言权。但是,我想知道我应该做什么而不是:def__init__(self,output_file=None,output_dir=None):"""Setthefrobnicatorup,alongwithdefaultgeometries"""self.margin=30self.pos=[0,0]self.sep=[5,5]self.cell=[20

python - 类型错误 : 'tuple' object does not support item assignment when swapping values

我正在用python编写一个简单的排序程序并遇到此错误。我想交换列表元素,但它返回一个错误。我在下面附上了有问题的错误和程序。list[i+1]=list[i]TypeError:'tuple'objectdoesnotsupportitemassignment代码:defmy_sort(list):forindexinrange(1,len(list)):value=list[index]i=index-1whilei>=0:ifvalue 最佳答案 评估“1,2,3”会产生(1,2,3),一个tuple。正如您所发现的,元组是不

python - 属性错误 : Module Pip has no attribute 'main'

我正在尝试为名为Zulip的开源项目构建pythonapi,但我一直遇到与下面的屏幕截图所示相同的问题。我正在运行python3,我的pip版本是10.0.0。有问题的文件是setup.py并且搞砸的代码是访问pip.main()属性以安装包时。现在,我知道这个构建应该会成功,因为它是一个开源项目,但我已经尝试了几个小时来解决关于pip.main()的依赖问题。任何帮助将不胜感激。 最佳答案 python3-mpipinstall--user--upgradepip==9.0.3pipissue:rollback