How can I annotate a class in pypy?
I am using pypy to translate some python script to C language. Say that I have a python class like this:
class A:
def __init__(self):
self.a = 0
def func(self):
pass
I notice that A.func
is a unbound method rather than a function so that it cannot be translated by pypy. So I change the code slightly:
def func(self):
pass
class A:
def __init__(self):
self.a = 0
A.func = func
def target(*args):
return func, None
Now func
seems to be able to be translate开发者_如何学God by pypy. However when I try translate.py --source test.py
, an exception [translation:ERROR] TypeError: signature mismatch: func() takes exactly 2 arguments (1 given)
is raised. I notice that it might because I haven't annotate self
argument yet. However this self
have type A, so how can I annotate a class?
Thank you for your reading and answer.
Essentially PyPy's entry point is a function (accepting sys.argv usually as an argument). Whatever this function calls (create objects, call methods) will get annotated. There is no way to annotate a class, since PyPy's compiled code does not export this as API, but rather as a standalone program.
You might want to for example:
def f():
a = A()
a.func()
or even:
a = A()
def f():
a.func()
in which case a is a prebuilt constant.
Are you wanting a staticmethod or a classmethod?
精彩评论