magic-methods

What are the reasons for using type(obj).method() instead of obj.method()?

What are the reasons for using type(obj).method() instead of obj.method()? Question: I’ve seen a couple of times methods being called on the type of an object instead of the object itself. What might the reasons for that be, especially with special methods? Example from documentation: "For instance, if a class defines a method named _getitem_(), …

Total answers: 1

How can I check if an identifier is dunder or class-private (i.e. will be mangled)?

How can I check if an identifier is dunder or class-private (i.e. will be mangled)? Question: I’m writing a project that gives advice about variable names, and I want it to tell if a name matches any of the reserved classes of identifiers. The first one ("private") is pretty straightforward, just name.startswith(‘_’), but dunder and …

Total answers: 1

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

List of all Python dunder/magic methods – Which ones do you need to implement to correctly proxy an object? Question: 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__, …

Total answers: 1

Python's Magic Method for representation of class?

Python's Magic Method for representation of class? Question: I have a custom class like: class foo(object): def __init__(self, name): self.__name = name def get_name(self): return self.__name What I want to do is to write test = foo("test") print test instead of test = foo("test") print test.get_name() What is the magic method for doing this? Asked …

Total answers: 3

How to overload Python's __bool__ method?

How to overload Python's __bool__ method in 2.x? Question: I thought this should print "False", why is it printing "True"? >>> class Foo(object): … def __bool__(self): … return False … >>> f = Foo() >>> if f: … print "True" … else: … print "False" … True >>> Asked By: dividebyzero || Source Answers: You …

Total answers: 1

Making a python user-defined class sortable, hashable

Making a python user-defined class sortable, hashable Question: What methods need to be overridden/implemented when making user-defined classes sortable and/or hashable in python? What are the gotchas to watch out for? I type dir({}) into my interpreter to get a list of methods on built-in dicts. Of those, I assume I need to some implement …

Total answers: 4

Python __call__ special method practical example

Python __call__ special method practical example Question: I know that __call__ method in a class is triggered when the instance of a class is called. However, I have no idea when I can use this special method, because one can simply create a new method and perform the same operation done in __call__ method and …

Total answers: 16

How does python numpy.where() work?

How does python numpy.where() work? Question: I am playing with numpy and digging through documentation and I have come across some magic. Namely I am talking about numpy.where(): >>> x = np.arange(9.).reshape(3, 3) >>> np.where( x > 5 ) (array([2, 2, 2]), array([0, 1, 2])) How do they achieve internally that you are able to …

Total answers: 4