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

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -