magic-methods

Python dictionary “plus-equal” behavior

北战南征 提交于 2021-02-04 15:31:31
问题 I'm trying to understand the exact mechanism behind updating a python dictionary using d[key] += diff . I have some helper classes to trace magic method invocations: class sdict(dict): def __setitem__(self, *args, **kargs): print "sdict.__setitem__" return super(sdict, self).__setitem__(*args, **kargs) def __delitem__(self, *args, **kargs): print "sdict.__delitem__" return super(sdict, self).__delitem__(*args, **kargs) def __getitem__(self, *args, **kargs): print "sdict.__getitem__" return

PHP Adding stylesheets to header

泄露秘密 提交于 2021-01-27 11:52:38
问题 Is there a way to add stylesheets to the header after including the header file? Say we have this code: class content { public $stylesheets = array(); public function addStylesheets($stylesheets) { if(!empty($stylesheets)) { if(!is_array($stylesheets)) $stylesheets = array($stylesheets); $this->stylesheets = array_merge($this->stylesheets, $stylesheets); } } public function getStylesheets() { $response = ''; foreach($this->stylesheets as $stylesheet) $response .= '<link rel="stylesheet" type=

List of all Python dunder methods - Which ones do you need to implement to correctly proxy an object?

ε祈祈猫儿з 提交于 2020-08-23 03:30:12
问题 I'm trying to create an object proxy. Attribute/property lookup can be done by simply implementing the __getattribute__ , __setattr__ and __delattr__ methods. However, other functionalities like len(x), x[], bool(x) require other dunder methods like __len__, __getitem__, __bool__ to be implemented. If you don't implement these on the proxy class, but the object you're proxying supports them, your proxy will be incomplete and cause runtime errors. I would therefore like to have a comprehensive

is there a magic method for sorted() in Python?

拜拜、爱过 提交于 2020-07-21 04:09:41
问题 I understand that there are magic methods in python that can be overwritten by classes to control the way certain built in functions treat the members of these classes. For example, the behavior of len() and str() can be overwritten via magic methods __len__() and __str__() : class EmptySet(object): def __len__(self): return 0 def __str__(self): return '[]' >>> e = EmptySet() >>> str(e) [] >>> len(e) 0 There are also __cmp__() and __ge__() , __le__() etc methods to control how these objects

is there a magic method for sorted() in Python?

依然范特西╮ 提交于 2020-07-21 04:07:05
问题 I understand that there are magic methods in python that can be overwritten by classes to control the way certain built in functions treat the members of these classes. For example, the behavior of len() and str() can be overwritten via magic methods __len__() and __str__() : class EmptySet(object): def __len__(self): return 0 def __str__(self): return '[]' >>> e = EmptySet() >>> str(e) [] >>> len(e) 0 There are also __cmp__() and __ge__() , __le__() etc methods to control how these objects

Is it possible to transform default class dunder methods into class methods?

半城伤御伤魂 提交于 2020-06-17 02:52:47
问题 To give you some context, yesterday I came across this post. I found the problem quite interesting, so I tried to find a solution that would keep the syntax as close as possible to what was asked. Here is what I came up with: class DummyCube: cubes = [] @classmethod def __getattribute__(cls, name): print('is this called?') attributes = [getattr(cube, name) for cube in cls.cubes] return attributes class Cube: def __init__(self, volume): DummyCube.cubes.append(self) self.volume = volume a =

Why does functools.lru_cache not cache __call__ while working on normal methods

倾然丶 夕夏残阳落幕 提交于 2020-04-16 05:14:21
问题 I have been trying to make functools.lru_cache instance specific as described in this answer, but their solution fails when used on the __call__ method. class test: def __init__(self): self.method = lru_cache()(self.method) self.__call__ = lru_cache()(self.__call__) def method(self, x): print('method', end=' ') return x def __call__(self, x): print('__call__', end=' ') return x b = test() # b.method is cached as expected print(b.method(1)) # method 1 print(b.method(1)) # 1 # __call__ is