type = "instance" instead of "dict"

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cruella DeVille

    type = "instance" instead of "dict"

    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(myDictiona ry) I get that the type is
    "instance", which makes no sense to me. What does that mean?
    Thanks

  • Kent Johnson

    #2
    Re: type = "instance& quot; instead of "dict&quot ;

    Cruella DeVille wrote:[color=blue]
    > 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(myDictiona ry) I get that the type is
    > "instance", which makes no sense to me. What does that mean?[/color]

    Maybe you have an instance of UserDict instead of a built-in dict?
    [color=blue][color=green][color=darkred]
    >>> type({})[/color][/color][/color]
    <type 'dict'>[color=blue][color=green][color=darkred]
    >>> from UserDict import UserDict
    >>> type(UserDict() )[/color][/color][/color]
    <type 'instance'>

    UserDict.UserDi ct is not iterable, though you can still use
    for k in myDictionary.ke ys()

    There is also UserDict.Iterab leUserDict which does support iteration.

    I don't think there is much reason to use UserDict in modern Python as
    dict can be subclassed.

    Kent

    Comment

    • Cruella DeVille

      #3
      Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

      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.wri teFile(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?)

      Comment

      • Kent Johnson

        #4
        Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

        Cruella DeVille wrote:[color=blue]
        > So what you are saying is that my class Dict is a subclass of Dict, and
        > user defined dicts does not support iteration?[/color]

        I don't know what your class Dict is, I was guessing. The built-in is
        dict, not Dict.[color=blue]
        >
        > 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?)[/color]

        Is this actual code or are you typing from memory?
        [color=blue]
        > # put things in favDict (works fine)
        > fileToWrite.wri teFile(favDict) # AttributeError: Dict instance has no
        > attribute 'itervalues'[/color]

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

        Kent

        Comment

        • Cruella DeVille

          #5
          Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

          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.__di ct__.iteritems( ) it worked
          better...
          what does the __dict__ mean?

          Comment

          • Steve Juranich

            #6
            Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

            Cruella DeVille wrote:
            [color=blue]
            > 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.__di ct__.iteritems( ) it worked
            > better...
            > what does the __dict__ mean?[/color]

            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.
            [color=blue]
            >From your earlier posts, it sounds like you've managed to get a[/color]
            "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

            Comment

            • Jonathan Gardner

              #7
              Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

              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.

              Comment

              • James Stroud

                #8
                Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                Cruella DeVille wrote:[color=blue]
                > 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(myDictiona ry) I get that the type is
                > "instance", which makes no sense to me. What does that mean?
                > Thanks
                >[/color]

                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

                Comment

                • Steve Holden

                  #9
                  Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                  James Stroud wrote:[color=blue]
                  > Cruella DeVille wrote:
                  >[color=green]
                  >>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(myDictiona ry) I get that the type is
                  >>"instance", which makes no sense to me. What does that mean?
                  >>Thanks
                  >>[/color]
                  >
                  >
                  > 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
                  >[/color]
                  With the minor exception that the second is valid Python, while the
                  first isn't:
                  [color=blue][color=green][color=darkred]
                  >>> class Dict({}):[/color][/color][/color]
                  ... 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[color=blue][color=green][color=darkred]
                  >>>[/color][/color][/color]

                  It's quite an interesting error message, though ;-)
                  [color=blue]
                  > 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!
                  >[/color]
                  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/

                  Comment

                  • Fredrik Lundh

                    #10
                    Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                    James Stroud wrote:
                    [color=blue]
                    > 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[/color]

                    it is ?
                    [color=blue][color=green][color=darkred]
                    >>> class Dict({}):[/color][/color][/color]
                    .... 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>



                    Comment

                    • James Stroud

                      #11
                      Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                      Fredrik Lundh wrote:[color=blue]
                      > James Stroud wrote:
                      >
                      >[color=green]
                      >>Perhaps you did not know that you can inheret directly from dict, which
                      >>is the same as {}. For instance:
                      >>
                      >>class Dict({}):
                      >> pass[/color][/color]

                      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.
                      [color=blue]
                      >[/color]

                      Comment

                      • Cruella DeVille

                        #12
                        Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                        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.

                        Comment

                        • Mel Wilson

                          #13
                          Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                          James Stroud wrote:[color=blue]
                          > Fredrik Lundh wrote:
                          >[color=green]
                          >> James Stroud wrote:
                          >>
                          >>[color=darkred]
                          >>> Perhaps you did not know that you can inheret directly from dict, which
                          >>> is the same as {}. For instance:
                          >>>
                          >>> class Dict({}):
                          >>> pass[/color][/color]
                          >
                          >
                          > 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.
                          >[color=green]
                          >>[/color][/color]

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

                          Could that have been it?

                          Mel.

                          Comment

                          • James Stroud

                            #14
                            Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                            Mel Wilson wrote:[color=blue]
                            > James Stroud wrote:
                            >[color=green]
                            >> Fredrik Lundh wrote:
                            >>[color=darkred]
                            >>> 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[/color]
                            >>
                            >>
                            >>
                            >> 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.
                            >>[color=darkred]
                            >>>[/color][/color]
                            >
                            > class D (type({})):
                            > '''This derivative of dictionary works.'''
                            >
                            > Could that have been it?
                            >
                            > Mel.[/color]

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

                            James

                            Comment

                            • Steven D'Aprano

                              #15
                              Re: type = &quot;instance& quot; instead of &quot;dict&quot ;

                              Cruella DeVille wrote:
                              [color=blue]
                              > This is off topic, but if read the documentation is the answere to
                              > everything why do we need news groups?[/color]

                              Because "read the documentation" is NOT the answer to
                              everything. However, it was the answer to your question.
                              [color=blue]
                              > 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).[/color]

                              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.
                              [color=blue]
                              > And since every
                              > language is well documented...[/color]

                              That certainly is not true.
                              [color=blue]
                              > there's no need for these groups.
                              >
                              > I wouldn't ask here without trying to find the solution on my own
                              > first.[/color]

                              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.

                              Comment

                              Working...