type =" instance"而不是“dict” [英] type = "instance" instead of "dict"
问题描述
我正在尝试实现一个bookmark-url程序,该程序接受用户
输入并将字符串放入字典中。不知怎的,我不能用
迭代类型为Dict的myDictionary {}
当我写print type(myDictionary)时,我得到的类型是
" instance",这对我没有意义。这是什么意思?
谢谢
I''m trying to implement a bookmark-url program, which accepts user
input and puts the strings in a dictionary. Somehow I''m not able to
iterate myDictionary of type Dict{}
When I write print type(myDictionary) I get that the type is
"instance", which makes no sense to me. What does that mean?
Thanks
推荐答案
Cruella DeVille写道:
Cruella DeVille wrote:
我正在尝试实现一个bookmark-url程序,该程序接受用户
输入并将字符串放入字典中。不知怎的,我无法重复我的Dict类型的词典{}
当我写print type(myDictionary)时,我得到的类型是
" instance",这对我没有意义。这是什么意思?
I''m trying to implement a bookmark-url program, which accepts user
input and puts the strings in a dictionary. Somehow I''m not able to
iterate myDictionary of type Dict{}
When I write print type(myDictionary) I get that the type is
"instance", which makes no sense to me. What does that mean?
也许你有一个UserDict实例而不是内置字典?
Maybe you have an instance of UserDict instead of a built-in dict?
type({})
< type''dict''>来自UserDict导入UserDict
类型(UserDict())
type({}) <type ''dict''> from UserDict import UserDict
type(UserDict())
< type''instance''>
UserDict.UserDict是不可迭代的,尽管你仍然可以在myDictionary.keys中使用
来表示k。
还有UserDict .IterableUserDict支持迭代。
我不认为在现代Python中使用UserDict有太多理由,因为
dict可以被子类化。 br />
Kent
<type ''instance''>
UserDict.UserDict is not iterable, though you can still use
for k in myDictionary.keys()
There is also UserDict.IterableUserDict which does support iteration.
I don''t think there is much reason to use UserDict in modern Python as
dict can be subclassed.
Kent
所以你说的是我的Dict类是Dict的子类,并且
用户定义的dicts不支持迭代?
我正在做的是我想将字典的内容写入
文件,并将字典(favDict)作为这样的参数发送:
favDict = Dict()< - 我自己的课程(或不?)
#放东西在favDict(工作正常)
fileToWrite.writeFile(favDict)#AttributeError:Di ct实例没有
属性''itervalues''
其中fileToWrite是写模式的文件指针
我可以发送吗一个字典作为参数,就像我在这里做的那样?
另一个问题:自我是什么? - 意思是什么?我很熟悉
java和php(就像java和php一样吗?)
So what you are saying is that my class Dict is a subclass of Dict, and
user defined dicts does not support iteration?
What I''m doing is that I want to write the content of a dictionary to a
file, and send the dictionary (favDict) as a parameter like this:
favDict = Dict() <-- my own class (or not?)
# put things in favDict (works fine)
fileToWrite.writeFile(favDict) # AttributeError: Dict instance has no
attribute ''itervalues''
where fileToWrite is a filepointer in write-mode
Can I send a dictionary as a parameter the way I do here?
And another issue: what does the "self" - thing mean? I''m familiar with
java and php (is it like the java and php- this?)
Cruella DeVille写道:
Cruella DeVille wrote:
所以你说的是我的类Dict是Dict的子类,而用户定义的dicts不支持迭代?
我不知道你的课堂Dict是什么,我猜。内置的是
dict,而不是Dict。
我正在做的是我想将字典的内容写入
文件,然后发送字典(favDict)作为这样的参数:
favDict = Dict()< - 我自己的类(或不是?)
这是实际代码还是你从内存中输入?
#把东西放在favDict中(工作正常)
fileToWrite.writeFile(favDict)#AttributeError:Dict实例没有
属性''itervalues''
So what you are saying is that my class Dict is a subclass of Dict, and
user defined dicts does not support iteration?
I don''t know what your class Dict is, I was guessing. The built-in is
dict, not Dict.
What I''m doing is that I want to write the content of a dictionary to a
file, and send the dictionary (favDict) as a parameter like this:
favDict = Dict() <-- my own class (or not?)
Is this actual code or are you typing from memory?
# put things in favDict (works fine)
fileToWrite.writeFile(favDict) # AttributeError: Dict instance has no
attribute ''itervalues''
您使用的是哪个版本的Python? itervalues()在Python 2.2和
之后。
Kent
What version of Python are you using? itervalues() is in Python 2.2 and
later.
Kent
这篇关于type =" instance"而不是“dict”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!