javascript to python

lkcl luke.leighton at googlemail.com
Fri Oct 3 07:16:34 EDT 2008


On Oct 3, 10:29 am, Bruno Desthuilliers <bruno.
42.desthuilli... at websiteburo.invalid> wrote:
> lkcl a écrit :> On Oct 2, 7:42 pm, Bruno Desthuilliers
> > <bdesth.quelquech... at free.quelquepart.fr> wrote:
> >> lkcl a écrit :
> Not 'mine' - I'm not the OP.

 whoops, yes - i missed that.  sorry!

> And as far as I'm concerned, the point is
> exactly here : it's doing "a little bit more" than the original code.

 yeah, i know.  and that "bit more" gets you a proper representation
of the python "class" concept.

 i was merely pointing out that if you want to _really_ translate the
original code into python - _really_ strictly - it's not actually
possible.  because python doesn't have the concept of non-prototyping
(but... see below: i believe i may stand corrected on that)


> [..snip..] and is
> actually useless for the OP's use case (else the OP's code wouldn't use
> litteral object notation but a full-blown prototype).

 i know :)

> > it's
> > overriding the "prototype" of dataListener, making it a true "class"
> > object.
>
> There's nothing like a notion of "class" in javascript - as you of
> course already know.

 okay,okay :)  class-like :)


> Indeed. But the point is that Python - while close to a prototype-based
> language in many aspects - is still class-based. The closer Python
> translation of the OP's javascript snippet is probably the one I gave
> using a 'class singleton' - that is, using the class itself as an
> object.

 oh is _that_ how you do it.  thanks.  i always wondered how you did
class singletons in python.

> python-source-to-javascript-source tool like pyjamas won't give you back
> the original javascript snippet (which is by no mean a criticism of
> pyjamas - it would just be way too complicated to automatize such a
> translation).

 well... you _say_ that... but... actually, if that's the real way to
represent class singletons, and it's an accurate representation of the
OP's javascript, and a good example, then _yes_, pyjs should
definitely have that added as a feature - to understand that a class
singleton _can_ get mapped to the much more efficient javascript
example you gave.

not that many people would _want_ to do that, so it goes onto the
"diminishing returns TODO list", but...

> Nope. You defined functions outside the object's scope, and you still
> have to instanciate dataListener. Also, this above code just won't work
> - unless you explicitely pass the dataListener instance to the
> functions, ie:
>
> d = dataListener()
> d.onDataAvailable(d, ...)

 yeah - i didn't realise what the python class singleton thing was.
> It seem you didn't read my other translation proprosal, so I repost it here:

> class dataListener(object):
>     data = ''

 i did - i just didn't understand its significance.

 so - to get this straight: when you do class clsname(object), and you
have the indentation and declaration of variables (e.g. data, above)
at the same level as the functions, it means that there's only one of
them? (i.e. a singleton)?

so, if i do this:

d = dataListener()
e = dataListener()

d.data = "fred"

print f.data

will return "fred"?

l.



More information about the Python-list mailing list