Why should i use python if i can use java

Alex Martelli aleaxit at yahoo.com
Thu Jun 7 05:19:50 EDT 2001


"Glyph Lefkowitz" <glyph at twistedmatrix.com> wrote in message
news:mailman.991892413.1443.python-list at python.org...
>
> On 6 Jun 2001, Chris Goringe wrote:
>
> !snip!
>
> > python doesn't [have ...]
> > strong typing (love it or hate it...)
>
> Hate it :)

What _I_ hate is the confusion between weak vs strong typing
AND static vs dynamic typechecks, though it's a classic one:-).
Weak typing, properly, is what (e.g.) Perl has, where you can
use any scalar wherever, and it will try to guess what you
mean, freely treating strings as numbers, viceversa, etc.
What Python has (like, say, Scheme) I'd call strong typing
that is dynamically enforced.  E.g.:

D:\py21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>> "ciao"[1.0]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: sequence index must be integer
>>>

That's *WEAK* typing?!  I call it pretty strong indeed --
and love it!  (Java works similarly here -- it's an
error to use a non-integer as array index -- although
C and C++ happily accept the float and truncate it...).
It's *DYNAMICALLY* checked and enforced, not statically.
I think it's more precise and useful to draw the
distinction as static vs dynamic than as strong
vs weak, no matter which way one wants to argue
for or against it.


> > interfaces (ditto)
>
> Well, python *does* have interfaces, at a much deeper level than Java; you

...but no way to specify them precisely and explicitly (not
that Java excels here, since it lacks preconditions and
postconditions on interfaces, sigh).  Anyway, PEPs 245 and
246 would help a lot with this, I think!


> > an elegant way of implementing singletons (or does it? anyone?)
>
> Aah, the singleton.  Global variables for the new millennium.  First of
> all, the singleton "pattern" is usually a bad idea.  If you have a class

*AMEN, HALLELUJAH*!  This makes two of us sharing this dislike
for the Singleton Design Pattern, so I'm not a Singleton any
more -- see, that's one typical issue with Singleton DP, what
starts out as a singleton often becomes not one:-).

> that can "only be instantiated once", then you're dealing with per-process
> state, and representing it as a class instance can be misleading.  If it's

Yes.  You may want to use instances because of flexible
arrangements you can make regarding all sorts of operations
(calls, attribute accesses/bindings, etc etc) through special
methods (in Python, at least:-).  But separating the two
issues is simpler and thus preferable: have the instances
(if instances are needed) be freely created, shared, deleted,
etc -- it suffices that the instances *DO NOT HAVE SPECIFIC
PER-INSTANCE STATE* but 'delegate' all of their state to
the "object appearing but once" (a "singleton" by some way
to mean this word, but not a singleton Design Pattern:-).


> Thankfully, Python provides a much better mechanism for doing so!
> "Modules" are actually singletons wearing funny shoes.  Any "singleton"
> state can be specified as a variable in a module. (Which is usually
> declared with the 'global' keyword.  Coincidence?)

Today you can also have an instance masquerading as a module,
as it happens -- delicate, but it does mean you can control
attribute setting and getting on a "module"!-)  Witness:

D:\py21>python
Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>> class AllAttrStrings:
...     def __setattr__(self, name, value):
...         if type(value)!=type(''):
...             raise TypeError,"Attributes must be strings"
...         self.__dict__[name]=value
...
>>> import sys
>>> sys.modules['AAS']=AllAttrStrings()
>>> import AAS
>>> AAS.foo='bar'
>>> AAS.foo
'bar'
>>> AAS.peep=23
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 4, in __setattr__
TypeError: Attributes must be strings
>>>

The "import AAS" references the instance-object that
is masquerading as a module, once the appropriate entry
in sys.modules is properly initialized, no matter what
other module IS importing AAS.  Now, used with care and
good taste of course, ISN'T this cool...?!-)


> > but most of all, it doesn't have
> >
> > J2EE
> > J2ME
>
> Actually it does!  With Jython, you can access any Java API from Python.
> Neat, huh?

Absolutely!  But does any commercial IDE, as you earlier
referenced, support Jython yet?  I don't know of any.


Alex






More information about the Python-list mailing list