Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Jython class names

Reply
Thread Tools

Jython class names

 
 
Robin Becker
Guest
Posts: n/a
 
      09-09-2003
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
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      09-09-2003
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.velocityreviews.com/forums/(E-Mail Removed) && 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
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      09-09-2003
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
 
Reply With Quote
 
Robin Becker
Guest
Posts: n/a
 
      09-09-2003
In article <bjk2fk$38o$07$(E-Mail Removed)-online.com>, Peter Otten
<(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-09-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FW: [Jython-users] Jython Licensing Question Pinegar, Kent T Python 0 07-24-2008 01:38 PM
FW: [Jython-users] ERROR : parsing xml in jython Divya Prakash Python 0 12-19-2006 02:49 PM
[Jython-users] ANN: SPIRO - a cPython->Jython bridge (amongst otheruses) David McNab Python 0 04-09-2004 10:20 AM
RE: Jython class names Michael Chermside Python 1 09-09-2003 07:18 PM
RE: Jython class names Michael Chermside Python 1 09-09-2003 05:29 PM



Advertisments