Jython class names

Discussion in 'Python' started by Robin Becker, Sep 9, 2003.

  1. Robin Becker

    Robin Becker Guest

    I recently came across a problem with porting to Jython

    basically the following is prints 1 in CPython, but not in Jython.

    class C:
    pass

    red = C()
    print red.__class__.__name__ is 'C'


    it seems that Jython doesn't intern names in the same way or instances
    are not constructed in the same way. Is the above test robust or should
    I always be using == for testing class names?
    --
    Robin Becker
     
    Robin Becker, Sep 9, 2003
    #1
    1. Advertising

  2. Robin Becker wrote:

    > I recently came across a problem with porting to Jython
    >
    > basically the following is prints 1 in CPython, but not in Jython.
    >
    > class C:
    > pass
    >
    > red = C()
    > print red.__class__.__name__ is 'C'
    >
    > it seems that Jython doesn't intern names in the same way or instances
    > are not constructed in the same way. Is the above test robust or
    > should
    > I always be using == for testing class names?


    You should be testing string equality with ==. Whether two strings that
    are equal are actually the same string (is) is an implementation detail.

    --
    Erik Max Francis && && http://www.alcyone.com/max/
    __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
    / \ I sidestep the either/.or choices of logic and choose both.
    \__/ Ken Feit
     
    Erik Max Francis, Sep 9, 2003
    #2
    1. Advertising

  3. Robin Becker

    Peter Otten Guest

    Robin Becker wrote:

    > I recently came across a problem with porting to Jython
    >
    > basically the following is prints 1 in CPython, but not in Jython.
    >
    > class C:
    > pass
    >
    > red = C()
    > print red.__class__.__name__ is 'C'
    >
    >
    > it seems that Jython doesn't intern names in the same way or instances
    > are not constructed in the same way. Is the above test robust or should
    > I always be using == for testing class names?


    I have no Jython around, but I expect the following to always work:

    >>> class C: pass

    ....
    >>> C().__class__.__name__ is C.__name__

    True
    >>> isinstance(C(), C)

    True
    >>>


    (Of course isinstance() will also return True for subclass instances)

    Peter
     
    Peter Otten, Sep 9, 2003
    #3
  4. Robin Becker

    Robin Becker Guest

    In article <bjk2fk$38o$07$-online.com>, Peter Otten
    <> writes
    >Robin Becker wrote:
    >
    >> I recently came across a problem with porting to Jython
    >>
    >> basically the following is prints 1 in CPython, but not in Jython.
    >>
    >> class C:
    >> pass
    >>
    >> red = C()
    >> print red.__class__.__name__ is 'C'
    >>
    >>
    >> it seems that Jython doesn't intern names in the same way or instances
    >> are not constructed in the same way. Is the above test robust or should
    >> I always be using == for testing class names?

    >
    >I have no Jython around, but I expect the following to always work:
    >


    I just tried and wow


    >>>> class C: pass

    >...
    >>>> C().__class__.__name__ is C.__name__

    >True


    it doesn't work

    C:\jython-2.1>jython
    Jython 2.1 on java1.4.2 (JIT: null)
    Type "copyright", "credits" or "license" for more information.
    >>> class C: pass

    ....
    >>> C().__class__.__name__ is C.__name__

    0
    >>> C().__class__.__name__ == C.__name__

    1
    >>>




    .......
    >
    >(Of course isinstance() will also return True for subclass instances)
    >
    >Peter

    We're using the name tests in case we get the class from different
    import paths.
    --
    Robin Becker
     
    Robin Becker, Sep 9, 2003
    #4
  5. Robin Becker

    Peter Otten Guest

    Robin Becker wrote:

    > C:\jython-2.1>jython
    > Jython 2.1 on java1.4.2 (JIT: null)
    > Type "copyright", "credits" or "license" for more information.
    >>>> class C: pass

    > ...
    >>>> C().__class__.__name__ is C.__name__

    > 0
    >>>> C().__class__.__name__ == C.__name__

    > 1
    >>>>


    That's definitely not what I expected. So == is the way to go.
    Still, I'm curious: would the code below return 100 in Jython?

    >>> class C: pass

    ....
    >>> many = [C() for i in range(100)]
    >>> d = {}
    >>> for c in many:

    .... d[id(c.__class__.__name__)] = None
    ....
    >>> len(d.keys())

    1

    Peter
     
    Peter Otten, Sep 9, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Chermside

    RE: Jython class names

    Michael Chermside, Sep 9, 2003, in forum: Python
    Replies:
    1
    Views:
    426
    Erik Max Francis
    Sep 9, 2003
  2. Michael Chermside

    RE: Jython class names

    Michael Chermside, Sep 9, 2003, in forum: Python
    Replies:
    1
    Views:
    339
    Robin Becker
    Sep 9, 2003
  3. David McNab
    Replies:
    0
    Views:
    499
    David McNab
    Apr 9, 2004
  4. Divya Prakash
    Replies:
    0
    Views:
    442
    Divya Prakash
    Dec 19, 2006
  5. Pinegar, Kent T

    FW: [Jython-users] Jython Licensing Question

    Pinegar, Kent T, Jul 24, 2008, in forum: Python
    Replies:
    0
    Views:
    355
    Pinegar, Kent T
    Jul 24, 2008
Loading...

Share This Page