Adding attributes to instancemethods in Python

For attribute lookup, Python is automatically using the real function attached to the instance method for you. For attribute setting, it is not. They are two separate operations depending on which side of the statement you’re on, even though they both use the . operator. When you access an instance method’s __func__, you’re manually accessing … Read more

How can I use functools.singledispatch with instance methods?

Update: As of Python 3.8, functools.singledispatchmethod allows single dispatch on methods, classmethods, abstractmethods, and staticmethods. For older Python versions, see the rest of this answer. Looking at the source for singledispatch, we can see that the decorator returns a function wrapper(), which selects a function to call from those registered based on the type of … Read more

How to understand the difference between class_eval() and instance_eval()?

As the documentation says, class_eval evaluates the string or block in the context of the Module or Class. So the following pieces of code are equivalent: class String def lowercase self.downcase end end String.class_eval do def lowercase self.downcase end end In each case, the String class has been reopened and a new method defined. That … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)