Hash of class from instance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Joakim Storck

    Hash of class from instance

    Hello,

    Is there any way that I can find the hash value of a class from an
    instance?
    [color=blue][color=green][color=darkred]
    >>> class A:[/color][/color][/color]
    .... pass
    ....[color=blue][color=green][color=darkred]
    >>> a = A()
    >>> hash(A)[/color][/color][/color]
    10782976[color=blue][color=green][color=darkred]
    >>> hash(a)[/color][/color][/color]
    12251904[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    What I want is a function that returns the value of 'hash(A)':[color=blue]
    > a.getHashOfClas s()[/color]
    10782976

    Is this possible?

    /Joakim

  • Duncan Booth

    #2
    Re: Hash of class from instance

    Joakim Storck wrote:
    [color=blue]
    > Is there any way that I can find the hash value of a class from an
    > instance?
    >[/color]

    You only had to post the question once. It seems a strange thing to want,
    but just do:

    hash(a.__class_ _)

    Comment

    • Joakim Storck

      #3
      Re: Hash of class from instance

      Thanks!

      Not so strange I think, the hash values of classes will be used as keys
      in a dictionary that serve as an object pool.

      Sorry about the double posting, I got a 'server error' message the
      first time, so I figured it hadn't gone trhough.

      /Joakim

      Comment

      • Mick Krippendorf

        #4
        Re: Hash of class from instance

        Joakim Storck wrote:[color=blue]
        > [...] the hash values of classes will be used as
        > keys in a dictionary that serve as an object pool. [...][/color]

        That does seem unwise (as Teal'c would have uttered). The spec says:

        ----
        hash( object)

        Return the hash value of the object (if it has one). Hash values are
        integers. They are used to quickly compare dictionary keys during a
        dictionary lookup. Numeric values that compare equal have the same hash
        value (even if they are of different types, as is the case for 1 and
        1.0).
        ----

        Normally the following should hold (not mentioned in the above spec but
        true for Java, e.g.): if a and b are objects such that a equals b then
        hash(a) equals hash(b). This does not imply that if hash(a) equals
        hash(b) also a equals b. More formally: (a == b) -> (hash(a) ==
        hash(b)).

        In Python there seems to be no guarantee that different objects also
        have different hash values. So let's assume we have class objects Foo
        and Bar, which by some unlikely incident happen to have the same hash
        values, then storing them in a dictonary under their respective hash
        values (which are identical) would most probably lead into a problem,
        secifically the problem that you'd end up accessing Foo when you indeed
        think you are accessing Bar, or vice versa. Just try this:
        [color=blue][color=green][color=darkred]
        >>> my_pseudo_hash_ code = 1
        >>> my_dict = {my_pseudo_hash _code:"gnarf",[/color][/color][/color]
        my_pseudo_hash_ code:"snarf"}[color=blue][color=green][color=darkred]
        >>> print my_dict[/color][/color][/color]

        Mick.

        Comment

        • Steven Bethard

          #5
          Re: Hash of class from instance

          Mick Krippendorf wrote:[color=blue]
          > In Python there seems to be no guarantee that different objects also
          > have different hash values.[/color]

          Well, it's true that you can override the __hash__ method to do whatever
          you want, but I believe the default for class __hash__ methods is to
          return the class id, which should be different for each class:

          py> class C(object):
          .... pass
          ....
          py> hash(C)
          12699152
          py> id(C)
          12699152

          Steve

          Comment

          • Joakim Storck

            #6
            Re: Hash of class from instance

            So I guess it might be a little bit less unwise to use id() instead
            then...

            /Joakim

            Comment

            • Just

              #7
              Re: Hash of class from instance

              In article <1107373593.789 905.168850@o13g 2000cwo.googleg roups.com>,
              "Joakim Storck" <joakim.storck@ home.se> wrote:
              [color=blue]
              > So I guess it might be a little bit less unwise to use id() instead
              > then...[/color]

              Why don't you use the class objects themselves as dict keys?

              Just

              Comment

              • Joakim Storck

                #8
                Re: Hash of class from instance

                Simply because it didn't occur to me. So now I have
                [color=blue][color=green][color=darkred]
                >>> class A: pass[/color][/color][/color]
                ....[color=blue][color=green][color=darkred]
                >>> d = {A:[A(),A(),A()]}
                >>> a = d[A].pop()
                >>> a[/color][/color][/color]
                <__main__.A instance at 0x00F09A58>[color=blue][color=green][color=darkred]
                >>>[/color][/color][/color]

                Thanks all!

                /Joakim

                Comment

                Working...