English 中文(简体)
python 中的“ 属性必须是字符串” 错误
原标题:gettattr , "attributes must be string" error in python

我正在尝试使用生成器在我的代码中使用 getattr 函数

li=[]
m=[method for method in dir(li) if callable(getattr(li,method))]
print getattr(li,(str(i) for i in m))

< 强度 > 错误 :

TypeError: getattr(): attribute name must be string

如果我对i使用字符串强制, 为什么出现这个错误?

而且,如果我使用代码

li=[]
m=[method for method in dir(li) if callable(getattr(li,method))]
for i in range(10):
    print getattr(li,str(m[i]))

那末,没有迷误,

我是新来的Python,如果我犯了非常基本的错误 请原谅我,请有人详细解释一下这个错误。谢谢。

< 强势 > Edit : 这个代码的原理相同( 这是从 Dive 到 Python 的示例 ) 。 在此, 同样的事情也做了, 那么为什么没有错误?

def info(object, spacing=10, collapse=1):
    """Print methods and doc strings.

    Takes module, class, list, dictionary, or string."""
    methodList = [e for e in dir(object) if callable(getattr(object, e))]
    processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
    print "
".join(["%s %s" %
                     (method.ljust(spacing),
                      processFunc(str(getattr(object, method).__doc__)))
                     for method in methodList])
最佳回答

OK,鉴于您的编辑,我改变了我的答案。你似乎在期待发电机做与他们不同的事情。

您不将生成器传送到函数上,而是对生成器产生的每个项目进行函数工作,在生成器上环绕,然后在循环中执行您想要的函数。

然而,在这里你不需要发电机表达式,只需绕过列表,例如:

for method in m:
    print(getattr(li, method))

如果您想要使用生成器表达式, 您可以在这里使用它, 而不是首先构建列表 :

for method in (method for method in dir(li) if callable(getattr(li, method))):
    print(getattr(li, method))

尽管请注意,对于你试图在这里做的事情, Inspect 模块 可以帮助避免很多你正在做的事情。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签