יום שלישי, 15 באפריל 2014

python __get__ magic method

I used the __get__ python magic method in order to implement  explicit conversion of class :
The following example converts a string wrapper class into the string length:

class TheConverter:
    def __init__ (self):
        pass
    def __get__ (self, instance, owner):       
        return len ( instance.mValue)
   
class ToGetConverted:
    def __init__ (self ,pValue ):
        self.mValue = pValue
    LenConvertor = TheConverter()
print (ToGetConverted("Hands Up").LenConvertor)
if __name__ == '__main__':
    pass

The sample returns : 8

References:
magic words


 


אין תגובות:

הוסף רשומת תגובה