Transcrypt icon indicating copy to clipboard operation
Transcrypt copied to clipboard

Descriptor Protocol

Open jnnkB opened this issue 5 years ago • 4 comments

I couldn't find any information on this, but is the Descriptor Protocol supported. Or is there some way to archive something like this?

jnnkB avatar Jul 10 '20 13:07 jnnkB

search __get__ at https://docs.python.org/3/reference/datamodel.html

thautwarm avatar Jul 22 '20 21:07 thautwarm

The descriptor protocol is not supported. Please explain what you want to achieve, so I can better help you find a workaround.

JdeH avatar Nov 02 '20 08:11 JdeH

Hey, I'm sorry for not having answered yet.

When I opened this issue I was looking for a way on how I could do something like this:

class Document:
    def write(self, *args, **kwargs):
        print(*args, **kwargs)


document = Document()


class State(object):
    def __init__(self, initval):
        self.val = initval

    def __get__(self, obj, objtype):
        return self.val

    def __set__(self, obj, val):
        self.val = val
        obj.update()


class View:
    def __init__(self):
        document.write(self.render())

    def update(self):
        document.write(self.render())


class Main(View):
    number = State(0)

    def click_handler(self):
        self.number = 1

    def render(self):
        return f"The number is {self.number}"


main = Main()
main.click_handler()

It's nothing I really need. I just was trying around whats was possible. And I thought it might be interesting for implementing a simple fronted framework in python. But I guess using a function like setState() would be way easier and much more perfoment.

jnnkB avatar Nov 07 '20 16:11 jnnkB

@JdeH Do callable class instances (instances of classes with a __call__ method) behave properly in Transcrypt as bound instance methods even if their __get__ isn't called?

mentalisttraceur avatar Jun 01 '22 05:06 mentalisttraceur