Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python descriptor protocol analog in other languages?

Is there something like the Python descriptor protocol implemented in other languages? It seems like a nice way to increase modularity/encapsulation without bloating your containing class' implementation, but I've never heard of a similar thing in any other languages. Is it likely absent from other languages because of the lookup overhead?

like image 432
cdleary Avatar asked Aug 29 '08 09:08

cdleary


People also ask

What is descriptor protocol in Python?

Descriptors are a powerful, general purpose protocol. They are the mechanism behind properties, methods, static methods, class methods, and super() . They are used throughout Python itself.

What is __ set __ in Python?

The __set__() method is invoked when the value is set to the attribute, and unlike the __get__() method, it returns nothing. It has two arguments apart from the descriptor object itself, i.e., the instance which is the same as the __get__() method and the value argument, which is the value you assign to the attribute.

What is the difference between properties and descriptors?

The Cliff's Notes version: descriptors are a low-level mechanism that lets you hook into an object's attributes being accessed. Properties are a high-level application of this; that is, properties are implemented using descriptors.

What is descriptor object in Python?

Descriptors are Python objects that implement a method of the descriptor protocol, which gives you the ability to create objects that have special behavior when they're accessed as attributes of other objects.


1 Answers

I've not heard of a direct equivalent either. You could probably achieve the same effect with macros, especially in a language like Lisp which has extremely powerful macros.

I wouldn't be at all surprised if other languages start to incorporate something similar because it is so powerful.

like image 183
Andrew Wilkinson Avatar answered Oct 13 '22 19:10

Andrew Wilkinson