type = "instance" instead of "dict"

James Stroud jstroud at ucla.edu
Tue Feb 28 00:37:18 EST 2006


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



More information about the Python-list mailing list