Python equivalent of C++ member pointer -
what equivalent of c++ member pointer in python? basically, able replicate similar behavior in python:
// pointer member of myclass int (myclass::*ptmember)(int) = &myclass::member; // call member on instance, e.g. inside function // member pointer passed instance.*ptmember(3)
follow question, if member property instead of method? possible store/pass "pointer" property without specifying instance?
one way pass string , use eval
. there cleaner way?
edit: there several answers, each having useful offer depending on context. ended using described in answer, think other answers helpful whoever comes here based on topic of question. so, not accepting single 1 now.
assuming python class:
class myclass: def __init__(self): self.x = 42 def fn(self): return self.x
the equivalent of c++ pointer-to-memberfunction this:
fn = myclass.fn
you can call using instance in c++:
o = myclass() print(fn(o)) # prints 42
however, more interesting thing fact can take "address" of bound member function, doesn't work in c++:
o = myclass() bfn = o.fn print(bfn()) # prints 42,
concerning follow-up properties, there plenty answers here address issue, provided still one.
Comments
Post a Comment