getting started with classesSee original GitHub issue
Am I doing this right?
class Number(Class, Final): value = Member(int) def __init__(self, val): self.value = val def __add__(self, other): value = self.value + other.value return Number(value) @Entrypoint def main(): a = Number(77) b = Number(2) for i in range(10000000): c = a+b a = c print(c)
@Entrypoint it runs without any llvm magic, is that right?
When I do add the
@Entrypoint then everything gets compiled (or the compiler barfs) ? Is that right? I’d like to know if it ever sneaks back into the python interpreter, like how cython does.
Here is a second attempt, where I try overloading
__add__. It fails to compile.
Number = Forward("Number") class Number(Class, Final): value = Member(int) def __init__(self, val): self.value = val def __str__(self): return str(self.value) def __add__(self, other:Number): value = self.value + other.value return Number(value) def __add__(self, other:int): value = self.value + value return Number(value)
Also, without the Final, it complains that I haven’t annotated a return type for
__add__. Can I annotate that
__add__ returns a Number ? Or do I just stick with Final?
I’m glad there is a comprehensive test suite, but lines like this:
__add__ = lambda self, other: C("add") make no sense to me. Maybe someone should add a directory of example code ? Examples that don’t compile/run would also be good to see.
- Created 2 years ago
Top GitHub Comments
On Thu, Feb 25, 2021 at 2:24 PM Simon Burton firstname.lastname@example.org wrote:
Separately, on string formatting, I guess we didn’t explicitly model str.format. fstrings work although we have not implemented all the twiddly little formatters