Which special methods bypasses __getattribute__ in Python?

You can find an answer in the python3 documentation for object.__getattribute__, which states: Called unconditionally to implement attribute accesses for instances of the class. If the class also defines __getattr__(), the latter will not be called unless __getattribute__() either calls it explicitly or raises an AttributeError. This method should return the (computed) attribute value or … Read more

How to document magic (_call and _callStatic) methods for IDEs

Use class-level PHPDoc comment — specifically @method tag — works fine in PhpStorm: /** * @method static someClass get_by_user_id(int $id) Bla-bla * @method static someClass get_first_by_id(int $id) */ abstract class a { … In the above: @method — PHPDoc tag static — tells that this is static method someClass or $this — return type get_by_user_id … Read more

Simple example of use of __setstate__ and __getstate__

Here’s a very simple example for Python that should supplement the pickle docs. class Foo(object): def __init__(self, val=2): self.val = val def __getstate__(self): print(“I’m being pickled”) self.val *= 2 return self.__dict__ def __setstate__(self, d): print(“I’m being unpickled with these values: ” + repr(d)) self.__dict__ = d self.val *= 3 import pickle f = Foo() f_data … Read more

Why isn’t my class initialized by “def __int__” or “def _init_”? Why do I get a “takes no arguments” TypeError, or an AttributeError?

What do the exception messages mean, and how do they relate to the problem? As one might guess, a TypeError is an Error that has to do with the Type of something. In this case, the meaning is a bit strained: Python also uses this error type for function calls where the arguments (the things … Read more

Assigning (instead of defining) a __getitem__ magic method breaks indexing [duplicate]

Special methods (essentially anything with two underscores on each end) have to be defined on the class. The internal lookup procedure for special methods completely skips the instance dict. Among other things, this is so if you do class Foo(object): def __repr__(self): return ‘Foo()’ the __repr__ method you defined is only used for instances of … Read more

scala slick method I can not understand so far

[UPDATE] – added (yet another) explanation on for comprehensions The * method: This returns the default projection – which is how you describe: ‘all the columns (or computed values) I am usually interested’ in. Your table could have several fields; you only need a subset for your default projection. The default projection must match the … Read more