jjzjj

python - 为什么不在隐式 __getitem__ 调用中调用 __getattribute__?

在尝试包装任意对象时,我遇到了字典和列表的问题。经过调查,我设法想出了一段我根本不理解其行为的简单代码。我希望你们中的一些人能告诉我发生了什么:>>>classCl(object):#simpleclassthatprints(andsuppresses)eachattributelookup...def__getattribute__(self,name):...print'Access:',name...>>>i=Cl()#instanceofclass>>>i.test#testthat__getattribute__overrideworksAccess:test>>>i.__

python - 类型错误 : 'generator' object has no attribute '__getitem__'

我写了一个应该返回字典的生成函数。但是,当我尝试打印一个字段时,出现以下错误printrow2['SearchDate']TypeError:'generator'objecthasnoattribute'__getitem__'这是我的代码fromcsvimportDictReaderimportpandasaspdimportnumpyasnpdefgenSearch(SearchInfo):forrow2inDictReader(open(SearchInfo)):yieldrow2train='minitrain.csv'SearchInfo='SearchInfo.csv'r

python - 类型错误 : 'function' object has no attribute '__getitem__'

这个问题不太可能帮助任何future的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visitthehelpcenter.关闭9年前。用python编写一些代码来评估一个基本函数。我有一个包含一些值的二维数组,我想将函数应用于每个值并获得一个新的二维数组:importnumpyasNdefmakeGrid(dim):'''Functiontoreturnagridofdistancesfromthecentreofanarray.Thisversionusesloopstofillthe

python - 为什么我会收到 "instance has no attribute ' __getitem_ _' "错误?

代码如下:classBinaryTree:def__init__(self,rootObj):self.key=rootObjself.left=Noneself.right=Noneroot=[self.key,self.left,self.right]defgetRootVal(root):returnroot[0]defsetRootVal(newVal):root[0]=newValdefgetLeftChild(root):returnroot[1]defgetRightChild(root):returnroot[2]definsertLeft(self,newNode):

python - 如何编写静态 python getitem 方法?

我需要更改什么才能使其正常工作?classA:@staticmethoddef__getitem__(val):return"Itworks"printA[0]请注意,我正在调用A类型的__getitem__方法。 最佳答案 当一个对象被索引时,特殊方法__getitem__首先在对象的类中查找。类本身就是一个对象,类的类通常是type。因此,要覆盖类的__getitem__,您可以重新定义其元类(使其成为type的子类):classMetaA(type):def__getitem__(cls,val):return"Itworks

python - TypeError 'x' 对象没有属性 '__getitem__'

我遇到了错误'CampSite'objecthasnoattribute'__getitem__'当我在管理界面中保存一个新的CampCon条目时。我有一个名为campsites的应用程序可以处理露营地数据库,还有另一个名为content的应用程序可以处理评论。我是Django的新手,这是我的第一个非教程项目。我一直在绞尽脑汁,在网上搜索没有运气的答案。预先感谢您的帮助。这是我的营地模型:fromdjango.contrib.gis.dbimportmodelsclassCampSite(models.Model):name=models.CharField(max_length=25

python - 带有数组的函数的 PyCharm getitem 警告

我从PyCharm收到代码检查警告。我理解其中的逻辑,但我不清楚修复它的适当方法。假设我有以下示例函数:defget_ydata(xdata):ydata=xdata**2foriinrange(len(ydata)):printydata[i]returnydata我收到2条警告:>>Expectedtype'Sized',got'int'instead(atline3)>>Class'int'doesnotdefine'__getitem__',sothe'[]'operatorcannotbeusedonitsinstances(atline4)函数的目的当然是解析一个numpy

python - 类型错误 : '_sre.SRE_Match' object has no attribute '__getitem__'

我目前遇到此错误,但不知道是什么意思。这是一个scrapypython项目,这是我看到的错误:File"/bp_scraper/bp_scraper/httpmiddleware.py",line22,infrom_crawlerreturncls(crawler.settings)File"/bp_scraper/bp_scraper/httpmiddleware.py",line12,in__init__ifparts[1]:TypeError:'_sre.SRE_Match'objecthasnoattribute'__getitem__'代码:importreimportran

python - 为什么 __getitem__ 不能是类方法?

假设下面的类:classClass(object):@classmethoddefgetitem(*args):print'getitem%s'%(args,)@classmethoddef__getitem__(*args):print'__getitem__%s'%(args,)getitem方法的行为符合预期:它接收Class作为第一个arg,但是__getitem__接收type作为第一个arg:callingClass.getitem(test)getitem(,'test')callingobj.getitem(test)getitem(,'test')callingCla

python - 我如何使用 __getitem__ 和 __iter__ 并从字典返回值?

我有一个带有字典的对象,我想通过__getitem__访问它并迭代(仅值,键无关紧要)但我不确定如何去做。例如:Python2.5.2(r252:60911,Jul222009,15:33:10)>>>classLibrary(object):...def__init__(self):...self.books={'title':object,'title2':object,'title3':object,}...def__getitem__(self,i):...returnself.books[i]...>>>library=Library()>>>library['title']