New attribute __convert__ for classes
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
On Sun, Jun 14, 2020 at 03:33:16PM -0000, artem6191
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/ phd@phdru.name Programmers don't die, they just GOSUB without RETURN.
On Sun, Jun 14, 2020 at 9:04 AM Oleg Broytman
On Sun, Jun 14, 2020 at 03:33:16PM -0000, artem6191 < artem129871@gmail.com> wrote:
This attribute will use for convertion classes to other types, i.e. int(MyClass) will return __convert__ result in MyClass Define ``__int__`` or ``__index__``.
or __float__ But the problem is that we need a dunder for each type you want to support. I think the idea here is that you could specify, in your own custom class, which type(s) you can convert to, and it could be any type. That being said, to the OP: what are your use cases here? adding a dunder requires compelling use cases, and in this case, why having a dunder would be more helpful that simply writing a convert to this type method on your class. And when would that dunder be called? I'm having trouble figuring that out. -CHB
See https://docs.python.org/3/reference/datamodel.html#special-method-names
Oleg. -- Oleg Broytman https://phdru.name/ phd@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-leave@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/PTAVFY... Code of Conduct: http://python.org/psf/codeofconduct/
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython
On 2020-06-14 12:33 p.m., artem6191 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
This is wrong. Do not use assert like this.
_______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/IOJVYB... Code of Conduct: http://python.org/psf/codeofconduct/
zope.interface provides an implementation of PEP 246. That PEP defines a `__conform__` method objects can use to adapt themselves to a prototype:
from zope.interface.common.numbers import IIntegral class MyClass: ... def __init__(self, number: int): ... self.number = number ... def __conform__(self, prototype): ... if prototype is IIntegral: ... return self.number * 5 ... IIntegral(MyClass(1)) 5
participants (5)
-
artem6191
-
Christopher Barker
-
Jason Madden
-
Oleg Broytman
-
Soni L.