By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,265 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,265 IT Pros & Developers. It's quick & easy.

type = "instance" instead of "dict"

P: n/a
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

Feb 27 '06 #1
Share this Question
Share on Google+
15 Replies


P: n/a
Cruella DeVille wrote:
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?


Maybe you have an instance of UserDict instead of a built-in dict?
type({}) <type 'dict'> from UserDict import UserDict
type(UserDict())

<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
Feb 27 '06 #2

P: n/a
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?)

Feb 27 '06 #3

P: n/a
Cruella DeVille wrote:
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'


What version of Python are you using? itervalues() is in Python 2.2 and
later.

Kent
Feb 27 '06 #4

P: n/a
I created a class Dict (not dict), but since it only complicates things
for me I implemented my program without it.

when I wrote myDictionary = dictionary.__dict__.iteritems() it worked
better...
what does the __dict__ mean?

Feb 27 '06 #5

P: n/a
Cruella DeVille wrote:
I created a class Dict (not dict), but since it only complicates things
for me I implemented my program without it.

when I wrote myDictionary = dictionary.__dict__.iteritems() it worked
better...
what does the __dict__ mean?
This is something else entirely and almost *certainly* not what you want.
Here's the skinny on how to subclass the built-in `dict' type.

class Dict(dict):
# Your mods go here.
From your earlier posts, it sounds like you've managed to get a

"classic" (i.e., old, almost deprecated) class. Change your class
definition to look more like what I have above and things should start
working.

If you're willing to post the code, we could pinpoint the problems much
faster.

--
Steve Juranich
Tucson, AZ
USA

Feb 27 '06 #6

P: n/a
You should probably spend a bit more time in the documentation reading
things for yourself. Read
http://www.python.org/doc/2.4.2/ref/types.html under "Class Instances"
for your answer.

Feb 28 '06 #7

P: n/a
Cruella DeVille wrote:
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


Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass

Is the same as

class Dict(dict):
pass

Now Dict can do everything that dict ({}) can do, but you can also
specialize it:

py> class Dict(dict):
.... def __str__(self):
.... return "I am %s long. But you should read the tutorial!" % len(self)
....
py> d = Dict()
py> d['a'] = 1
py> d['b'] = 2
py>
py> d['a']
1
py> d['b']
2
py> print d
I am 2 long. But you should read the tutorial!

James
Feb 28 '06 #8

P: n/a
James Stroud wrote:
Cruella DeVille wrote:
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
Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass

Is the same as

class Dict(dict):
pass

With the minor exception that the second is valid Python, while the
first isn't:
class Dict({}): ... pass
...
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: Error when calling the metaclass bases
dict expected at most 1 arguments, got 3


It's quite an interesting error message, though ;-)
Now Dict can do everything that dict ({}) can do, but you can also
specialize it:

py> class Dict(dict):
... def __str__(self):
... return "I am %s long. But you should read the tutorial!" % len(self)
...
py> d = Dict()
py> d['a'] = 1
py> d['b'] = 2
py>
py> d['a']
1
py> d['b']
2
py> print d
I am 2 long. But you should read the tutorial!

You're right about that, but we all need a helping hand now and then ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Feb 28 '06 #9

P: n/a
James Stroud wrote:
Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass

Is the same as

class Dict(dict):
pass


it is ?
class Dict({}):

.... pass
....
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Error when calling the metaclass bases
dict expected at most 1 arguments, got 3

</F>

Feb 28 '06 #10

P: n/a
Fredrik Lundh wrote:
James Stroud wrote:

Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass

I must have been hallucinating. I swear I did this before and it worked
just like class Dict(dict). Since it doesn't read as well, I've never
used it.

Feb 28 '06 #11

P: n/a
This is off topic, but if read the documentation is the answere to
everything why do we need news groups? The problem with the
documentation for Python is that I can't find what I'm looking for (and
I didn't even know what I was looking for either). And since every
language is well documented... there's no need for these groups.

I wouldn't ask here without trying to find the solution on my own
first.

Mar 1 '06 #12

P: n/a
James Stroud wrote:
Fredrik Lundh wrote:
James Stroud wrote:

Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass

I must have been hallucinating. I swear I did this before and it worked
just like class Dict(dict). Since it doesn't read as well, I've never
used it.


class D (type({})):
'''This derivative of dictionary works.'''

Could that have been it?

Mel.
Mar 1 '06 #13

P: n/a
Mel Wilson wrote:
James Stroud wrote:
Fredrik Lundh wrote:
James Stroud wrote:
Perhaps you did not know that you can inheret directly from dict, which
is the same as {}. For instance:

class Dict({}):
pass


I must have been hallucinating. I swear I did this before and it
worked just like class Dict(dict). Since it doesn't read as well, I've
never used it.


class D (type({})):
'''This derivative of dictionary works.'''

Could that have been it?

Mel.


This rings a bell. It was a couple of years ago, so the "type" must have
faded from my already murky memory.

James
Mar 1 '06 #14

P: n/a
Cruella DeVille wrote:
This is off topic, but if read the documentation is the answere to
everything why do we need news groups?
Because "read the documentation" is NOT the answer to
everything. However, it was the answer to your question.
The problem with the
documentation for Python is that I can't find what I'm looking for (and
I didn't even know what I was looking for either).
Is that a problem with the docs or with you? Or perhaps
a little of both?

In any case, now Jonathan Gardner has kindly pointed
you at the correct part of the docs, you will be able
to read up on it and have a better idea of what to do
next time.
And since every
language is well documented...
That certainly is not true.
there's no need for these groups.

I wouldn't ask here without trying to find the solution on my own
first.


Good. And now, because people didn't just answer your
immediate question, but pointed you at the part of the
docs where you can learn things you should know, you
may find it easier to solve future problems.

--
Steven.

Mar 1 '06 #15

P: n/a

"Steven D'Aprano" <st***@REMOVEMEcyber.com.au> wrote in message
news:44**************@REMOVEMEcyber.com.au...
Cruella DeVille wrote:
This is off topic, but if read the documentation is the answere to
everything why do we need news groups?


Because "read the documentation" is NOT the answer to
everything. However, it was the answer to your question.


Google can also answer some questions posted here in less time than it
takes to post here.
I wouldn't ask here without trying to find the solution on my own
first.


Unfortunately, such enterprise is not universal. More than once, someone
has asked something like "Hey, Newbie here. Are there any Python programs
that can mung frobits?" So I type "Python program mung frobits" into the
Google bar of my browser and an answer pops up in a second.

tjr

Mar 1 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.