Jython class names

R

Robin Becker

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?
 
E

Erik Max Francis

Robin said:
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.
 
P

Peter Otten

Robin said:
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:

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

Peter
 
R

Robin Becker

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

I just tried and wow


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.
.......

(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.
 
P

Peter Otten

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

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
....1

Peter
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top