overloading

Python Typing: Mypy errors with overload "overlap" when signatures are different

Python Typing: Mypy errors with overload "overlap" when signatures are different Question: The following code appears to generate two mypy errors: Overloaded function signatures 1 and 3 overlap with incompatible return types and Overloaded function signatures 2 and 3 overlap with incompatible return types; but all overloads have different signatures – Literal[True], Literal[False] and None …

Total answers: 2

Overloading Python numpy.ufunc as class method

Overloading Numpy functions? Question: I am hoping for some clarification on overloading Numpy universal functions in class methods. To illustrate, here is a class myreal with an overloaded cos method. This overloaded method calls cos imported from the math module. from math import cos class myreal: def __init__(self,x): self.x = x def cos(self): return self.__class__(cos(self.x)) …

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

Decorator for overloading in Python

Decorator for overloading in Python Question: I know it’s not Pythonic to write functions that care about the type of the arguments, but there are cases when it’s simply impossible to ignore types because they are handled differently. Having a bunch of isinstance checks in your function is just ugly; is there any function decorator …

Total answers: 4

Overloaded functions in Python

Overloaded functions in Python Question: Is it possible to have overloaded functions in Python? In C# I would do something like void myfunction (int first, string second) { # Some code } void myfunction (int first, string second, float third) { # Some different code } And then when I call the function it would …

Total answers: 6

Methods with the same name in one class in Python

Methods with the same name in one class in Python Question: How can I declare a few methods with the same name, but with different numbers of parameters or different types in one class? What must I change in the following class? class MyClass: """""" #———————————————————————- def __init__(self): """Constructor""" def my_method(self,parameter_A_that_Must_Be_String): print parameter_A_that_Must_Be_String def my_method(self,parameter_A_that_Must_Be_String,parameter_B_that_Must_Be_String): …

Total answers: 11

how to "reimport" module to python then code be changed after import

how to "reimport" module to python then code be changed after import Question: I have a foo.py def foo(): print “test” In IPython I use: In [6]: import foo In [7]: foo.foo() test Then I changed the foo() to: def foo(): print “test changed” In IPython, the result for invoking is still test: In [10]: …

Total answers: 5

How do I override delete() on a model and have it still work with related deletes

How do I override delete() on a model and have it still work with related deletes Question: I’m having a problem because I’m deleting a Widget by using some_widget_instance.delete(). I also have a model called WidgetFile with an override delete() method so that I can delete files off my hard drive when a WidgetFile is …

Total answers: 9

Why does defining __getitem__ on a class make it iterable in python?

Why does defining __getitem__ on a class make it iterable in python? Question: Why does defining __getitem__ on a class make it iterable? For instance if I write: class b: def __getitem__(self, k): return k cb = b() for k in cb: print k I get the output: 0 1 2 3 4 5 6 …

Total answers: 6