type = "instance" instead of "dict"

Steve Holden steve at holdenweb.com
Tue Feb 28 01:46:18 EST 2006


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/




More information about the Python-list mailing list