【类的简介和属性介绍】教程文章相关的互联网学习教程文章

python – 模块对象没有属性’create_frame’【代码】

在一些类似的问题中,这个特定的问题要么没有通过建议的解决方案解决,要么问题已经摇摆到不同的主题.因此,我不得不问这个问题: 返回的错误是:Traceback (most recent call last):File "learn.py", line 8, in <module>frame = simplegui.create_frame("Home", 300, 200) AttributeError: 'module' object has no attribute 'create_frame'这与以下代码有关import simplegui message = "Welcome!" def click():global messagemessa...

Python:我应该使用常量还是属性?【代码】

以下哪两种方法被认为是最佳做法?两者都达到了相同的效果.class Foo():LABELS = ('One','Two','Three')class Bar():def __init__(self):self.__labels = ('One','Two','Three')@propertydef labels(self):return self.__labels解决方法:如果您不需要自定义获取或设置行为,那么制作某个属性是没有意义的.第一个版本更好. 而且,这些行为并不完全相同.在Foo中,标签有一个类级别属性.在酒吧,没有.引用Foo.LABELS将正常工作,引用Bar.la...

python – 如何动态地向接口添加属性【代码】

我需要为接口中的每个属性添加一个属性.所以我试图动态修改它来添加它们,但现在还没有取得多大成功. 假设我有以下界面:class IMember(Interface):first_name = schema.TextLine(title=u'first name')last_name = schema.TextLine(title=u'last name')我想像这样修改它:class IMember(Interface):first_name = schema.TextLine(title=u'first name')last_name = schema.TextLine(title=u'last name')visbility_first_name = schem...

在Python中动态添加类__dict__属性【代码】

我试图将__dict__成员添加到由namedtuple生成的类. (__dict__出现在python 2.7.3中,但在2.7.5中删除.见http://bugs.python.org/issue15535.它存在并记录在python 3.3中.)我的代码使用vars(nametuple_object),它基于__dict__.我想在需要时补课. 这是我尝试过的:# Applies to Python 2.7.5 +C = namedtuple('C', ['x', 'y']) if not hasattr(C, '__dict__'):C.__dict__ = property(C._asdict)这不起作用,因为C继承__dict__,因此hasa...

在Python中打印类中对象的属性【代码】

参见英文答案 > Printing all instances of a class 6个此时我已经在Python中乱搞了大约一个半月,我想知道:有没有办法为该类中的所有对象打印一个类变量的值?例如(我正在开发一款迷你游戏):class potions:def __init__(self, name, attribute, harmstat, cost):self.name = nameself.attribute = attributeself.harmstat = harmstatself.cost = costLightning = potions("Lightning Potion"...

python – 使用asyncio.coroutine方法在类外部声明的asyncio event_loop失败,出现“AttributeError:’NoneType’对象没有属性’select【代码】

探索Python 3.4.0的asyncio模块,我试图创建一个带有asyncio.coroutine方法的类,这些方法是从类外部的event_loop调用的. 我的工作代码如下.import asyncioclass Foo():@asyncio.coroutinedef async_sleep(self):print('about to async sleep')yield from asyncio.sleep(1)@asyncio.coroutinedef call_as(self):print('about to call ass')yield from self.async_sleep()def run_loop(self):loop = asyncio.get_event_loop()loop.run...

python – Pylons重定向给出:AttributeError:’HTTPFound’对象没有属性’exception’

(这个问题记录了我对一个错误的研究,以节省其他人花时间解决它.) 我有一个基于Pylons 1.0.1框架的网页. 开发人员建议迁移到他们的新产品Pyramids的升级路径. Pylons 1.0.1将不会做进一步的工作. 但是,我不需要任何进一步的功能 – 网站正在维护,所以我没有升级框架. 最近,在将一些其他更改部署到服务器之后,尽管我的代码没有任何更改,但网站仍停止工作.特别是,重定向调用给出了错误:AttributeError: ‘HTTPFound’ object has no ...

python – AttributeError:’builtin_function_or_method’对象没有属性’replace’【代码】

当我尝试在我的程序中使用它时,它表示存在属性错误'builtin_function_or_method' object has no attribute 'replace'但我不明白为什么.def verify_anagrams(first, second):first=first.lowersecond=second.lowerfirst=first.replace(' ','')second=second.replace(' ','')a='abcdefghijklmnopqrstuvwxyz'b=len(first)e=0for i in a:c=first.count(i)d=second.count(i)if c==d:e+=1return b==e解决方法:你需要在它之后放置()来调用...

Python – dir() – 如何区分函数/方法和简单属性?【代码】

dir()返回所有已定义名称的列表,但尝试调用我看到列出的函数只是为了发现它实际上是一个属性,或尝试访问属性只是为了发现它实际上是一个可调用的,这很烦人.如何让dir()更具信息性?解决方法:要显示模块中已定义名称的列表,例如数学模块及其类型,您可以执行以下操作:[(name,type(getattr(math,name))) for name in dir(math)]getattr(math,name)从math模块返回对象(函数或其他),由变量“name”中的字符串值命名.例如type(getattr(m...

Python Curses – 模块’curses’没有属性’LINES’【代码】

我正在查看一本书中的一些源代码,并注意到一些代码似乎不在当前的Python2.7 API中.根据这段代码,模块curses应该有一个名为LINES的常量变量和另一个名为COLS的变量.我打开了一个Python交互式终端,发现没有COLS或LINES变量或方法. 我的问题是:这个代码甚至可以工作吗?def draw_loglines(self):self.screen.clear()status_col = 4bytes_col = 6 remote_host_col = 20status_start = 0 bytes_start = 4 remote_host_start = 10line_s...

python – Django民意调查App:AttributeError:’Choice’对象没有属性’question_text’【代码】

当我收到此错误时,我正在Django网站上进行教程.我使用的是OS X 10.10.>>> q.choice_set.create(choice_text='Not much', votes=0) Traceback (most recent call last):File "<console>", line 1, in <module>File "/Library/Python/2.7/site-packages/Django-1.7.8-py2.7.egg/django/db/models/base.py", line 458, in __repr__u = six.text_type(self)File "/Users/anushrutgupta/Documents/IMG/Django/mysite/polls/models.py", ...

xml到json,带有php或python的属性【代码】

我正在尝试将一些XML转换为JSON,这对于PHP来说很容易$file = file_get_contents('data.xml' ); $a = json_decode(json_encode((array) simplexml_load_string($file)),1); print_r($a);采用以下XML<?xml version="1.0" encoding="UTF-8"?> <foo><bar><one lang="fr" type="bar">Test</one><one lang="fr" type="foo">Test</one><one lang="fr" type="baz">Test</one></bar><thunk><thud><bar lang="fr" name="bob">test</bar><bar ...

python – 我怎样才能避免:“ZipFile实例在提取zip文件时没有属性’__exit__””?【代码】

代码是:import sys execfile('test.py')在test.py我有:import zipfile with zipfile.ZipFile('test.jar', 'r') as z:z.extractall("C:\testfolder")此代码生成:AttributeError ( ZipFile instance has no attribute '__exit__' ) # edited从python idle运行时,“test.py”中的代码有效.我正在运行python v2.7.10解决方法:嗨也许迟到但我只是为我解决这个错误:). 我在python 2.7上创建我的代码但是当我把它放在使用2.6的服务器上...

python – 在PyGObject中设置样式属性【代码】

我有一个非常简单的PyGObject应用程序:from gi.repository import Gtk, Gdkclass Window(Gtk.Window):def __init__(self, *args, **kwargs):super().__init__(*args, **kwargs)self.set_border_width(5)self.progress = Gtk.ProgressBar()self.progress.set_fraction(0.5)self.box = Gtk.Box()self.box.pack_start(self.progress, True, True, 0)self.add(self.box)self.connect('delete-event', Gtk.main_quit)self.show_all()wi...

python – AttributeError:模块’pydot’在spyder中没有属性’graph_from_dot_data’【代码】

我试图运行以下代码:from sklearn.datasets import load_iris from sklearn import tree import pydot clf = tree.DecisionTreeClassifier() iris = load_iris() clf = clf.fit(iris.data, iris.target) from sklearn.externals.six import StringIO from pydot import * dotfile = StringIO() tree.export_graphviz(clf, out_file = dotfile) pydot.graph_from_dot_data(dot_data.getvalue()).write_png("dtree2.png")我收到以下...