How to subclass Python list without type problems?

Firstly, I recommend you follow Björn Pollex’s advice (+1). To get past this particular problem (type(l2 + l3) == CustomList), you need to implement a custom __add__(): def __add__(self, rhs): return CustomList(list.__add__(self, rhs)) And for extended slicing: def __getitem__(self, item): result = list.__getitem__(self, item) try: return CustomList(result) except TypeError: return result I also recommend… pydoc … Read more

Reclassing an instance in Python

Reclassing instances like this is done in Mercurial (a distributed revision control system) when extensions (plugins) want to change the object that represent the local repository. The object is called repo and is initially a localrepo instance. It is passed to each extension in turn and, when needed, extensions will define a new class which … Read more

Python: Can a subclass of float take extra arguments in its constructor?

As float is immutable you have to overwrite __new__ as well. The following should do what you want: class Foo(float): def __new__(self, value, extra): return float.__new__(self, value) def __init__(self, value, extra): float.__init__(value) self.extra = extra foo = Foo(1,2) print(str(foo)) 1.0 print(str(foo.extra)) 2 See also Sub-classing float type in Python, fails to catch exception in __init__()

How to create a subclass in python that is inherited from turtle Module

Rounding up Ignacio’s answer and orokusaki’s comment you probably should write something like import turtle class TurtleGTX(turtle.Turtle): “””My own version of turtle””” def __init__(self,*args,**kwargs): super(TurtleGTX,self).__init__(*args,**kwargs) print(“Time for my GTX turtle!”) my_turtle = TurtleGTX() my_turtle.forward(100)