How to bind an unbound method without calling it?

All functions are also descriptors, so you can bind them by calling their __get__ method: bound_handler = handler.__get__(self, MyWidget) Here’s R. Hettinger’s excellent guide to descriptors. As a self-contained example pulled from Keith’s comment: def bind(instance, func, as_name=None): “”” Bind the function *func* to *instance*, with either provided name *as_name* or the existing name of … Read more

Python : Assert that variable is instance method?

inspect.ismethod is what you want to find out if you definitely have a method, rather than just something you can call. import inspect def foo(): pass class Test(object): def method(self): pass print inspect.ismethod(foo) # False print inspect.ismethod(Test) # False print inspect.ismethod(Test.method) # True print inspect.ismethod(Test().method) # True print callable(foo) # True print callable(Test) # True … Read more

Reflection MethodInfo.Invoke() catch exceptions from inside the method

EDIT: As I understand your issue, the problem is purely an IDE one; you don’t like VS treating the exception thrown by the invocation of the MethodInfo as uncaught, when it clearly isn’t. You can read about how to resolve this problem here: Why is TargetInvocationException treated as uncaught by the IDE? It appears to … Read more

codeigniter check for user session in every controller

Another option is to create a base controller. Place the function in the base controller and then inherit from this. To achieve this in CodeIgniter, create a file called MY_Controller.php in the libraries folder of your application. class MY_Controller extends Controller { public function __construct() { parent::__construct(); } public function is_logged_in() { $user = $this->session->userdata(‘user_data’); … Read more

to_s vs. to_str (and to_i/to_a/to_h vs. to_int/to_ary/to_hash) in Ruby

Note first that all of this applies to each pair of “short” (e.g. to_s/to_i/to_a/to_h) vs. “long” (e.g. to_str/to_int/to_ary/to_hash) coercion methods in Ruby (for their respective types) as they all have the same semantics. They have different meanings. You should not implement to_str unless your object acts like a string, rather than just being representable by … Read more

How would I cross-reference a function generated by autodoc in Sphinx?

You don’t need to add labels. In order to refer to a Python class, method, or other documented object, use the markup provided by the Python domain. For example, the following defines a cross-reference to the mymethod method: :py:meth:`mymodule.MyClass.mymethod` Or even simpler (since the Python domain is the default): :meth:`mymodule.MyClass.mymethod` The documentation of TextWrapper.wrap that … Read more

How Can I Pass a Member Function to a Function Pointer?

You can’t. You either pass a pointer to a static method or Parent has to accept also a pointer to the object. You might want to look at boost::bind and boost::function for that: #include <boost/bind.hpp> #include <boost/function.hpp> struct Y { void say(void) { std::cout << “hallo!”;} boost::function<void()> getFunc() { return boost::bind(&Y::say, this); } }; struct … Read more