Reputation: 51
I'm trying to define a function (initdeque()) that takes a pointer to an instance of the deque class. So this is what I tried:
from ctypes import *
class deque(Structure):
pass
deque._fields_ = [("left",POINTER(node)),("right",POINTER(node))]
def initdeque(class deque *p):
p.left=p.right=None
But this code gives a syntax error:
def initdeque(class deque *p):
^
SyntaxError: invalid syntax
What is the correct syntax?
Upvotes: 3
Views: 212
Reputation: 4381
Python (like Matlab) does not have explicit variable type definition. When a variable is created the compiler defines it's type judging from it's content. So (if I am not wrong) you can call the same function with multiple types as parameters and still work. For example
def fun(p):
return p=p*5
So u can call fun with a string as a parameter and return you a string that contains 5 times the string you sent. Or you can call it with an integer and the function will return you the result of the multiplication
Upvotes: 0
Reputation: 288090
There is no way to specify the type of Python variables, so the declaration of initdequeue
should just say:
def initdeque(p):
p.left = p.right = None
Instead of having a random function doing the initialization, you should consider using an initializer:
class deque(Structure):
_fields_ = [("left",POINTER(node)), ("right",POINTER(node))]
def __init__(self):
self.left = self.right = None
Now, to create a new instance, just write
p = dequeue()
Upvotes: 6