On Sun, Jun 14, 2020 at 03:33:16PM -0000, artem6191  <artem129...@gmail.com> 
wrote:
> This attribute will use for convertion classes to other types, i.e. 
> int(MyClass) will return __convert__ result in MyClass
> 
> Example:
> class MyClass:
>    def __init__(self, numer: int):
>       self.number = number
>    def __convert__(self, type):
>       assert type == int # Only integer is allowed
>       return self.number*5 # Example
> 
> test = MyClass(1)
> test_2 = MyClass(5)
> 
> print( int(test) ) # 5
> print( int(test2) ) # 25

   Define ``__int__`` or ``__index__``.

   See https://docs.python.org/3/reference/datamodel.html#special-method-names

Oleg.
-- 
    Oleg Broytman            https://phdru.name/            p...@phdru.name
           Programmers don't die, they just GOSUB without RETURN.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PTAVFYSXD4KTUYJCBWO7GB7F5QOH6Z5N/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to