type = "instance" instead of "dict"

C

Cruella DeVille

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
 
K

Kent Johnson

Cruella said:
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 '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
 
C

Cruella DeVille

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?)
 
K

Kent Johnson

Cruella said:
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
 
C

Cruella DeVille

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?
 
S

Steve Juranich

Cruella said:
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.
 
J

James Stroud

Cruella said:
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
 
S

Steve Holden

James said:
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:
... 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
 
F

Fredrik Lundh

James said:
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 ?
.... 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>
 
J

James Stroud

Fredrik said:
James Stroud wrote:

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.
 
C

Cruella DeVille

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.
 
M

Mel Wilson

James said:
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.
 
J

James Stroud

Mel said:
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
 
S

Steven D'Aprano

Cruella said:
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.
 
T

Terry Reedy

Steven D'Aprano said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
Virus9283

Latest Threads

Top