Code Monkey home page Code Monkey logo

Comments (4)

gordianknotC avatar gordianknotC commented on July 20, 2024

property getter can be done by some trick

def property(f):
    def empty():pass
    ref = f('init')
    attr = f.name
    ref.prototype['_$'+attr] = ref.prototype[attr]
    ref.prototype.__defineSetter__(attr,empty)
    ref.prototype.__defineGetter__(attr,ref.prototype['_$'+attr])

class Test
    def __init__(self):
        self.data = [1,2,3,4,5]

    @property
    def __length__(self,init):
        if init:return ABC
        else: return len(self.data)


a = Test()
a.__length__        # output: 5

from rapydscript.

atsepkov avatar atsepkov commented on July 20, 2024

I wish I had more time to look at this, will leave alone for now but would appreciate if someone else could look into tackling this to handle these in pythonic way. Another good alternative for creating these is defineProperty function:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty

from rapydscript.

kovidgoyal avatar kovidgoyal commented on July 20, 2024

Implemented in my fork: kovidgoyal/rapydscript-ng@e9a6bd3

from rapydscript.

atsepkov avatar atsepkov commented on July 20, 2024

Implemented in ES6 mode with standard ES6 syntax (this is closest to variant 1 described above):

class Element:
    def __init__(self, tag, attrib):
        self._children = []

    get length(self):
        return len(self._children)

Still on the fence about implementing the Python syntax. While this is not standard Python approach (doesn't look like Python community is fond of them: http://stackoverflow.com/questions/2627002/whats-the-pythonic-way-to-use-getters-and-setters), the get/set of ES6 seem to complement def better.

from rapydscript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.