new-style-class

Get class object __dict__ without special attributes

For getting all the defined class attributes I try to go with TheClass.__dict__ but that also gives me the special attributes. Is there a way to get only the self-defined attributes or do I have to "clean" the dict myself? ...

Making super() work in Python's urllib2.Request

This afternoon I spent several hours trying to find a bug in my custom extension to urllib2.Request. The problem was, as I found out, the usage of super(ExtendedRequest, self), since urllib2.Request is (I'm on Python 2.5) still an old style class, where the use of super() is not possible. The most obvious way to create a new class with ...

Python using derived class's method in parent class?

Can I force a parent class to call a derived class's version of a function? class Base(object): attr1 = '' attr2 = '' def virtual(self): pass # doesn't do anything in the parent class def func(self): print "%s, %s" % (self.attr1, self.attr2) self.virtual() and a class that derive...

Python: always use __new__ instead of __init__ ?

I understand how both __init__ and __new__ work. I'm wondering if there is anything __init__ can do that __new__ cannot? i.e. can use of __init__ be replaced by the following pattern: class MySubclass(object): def __new__(cls, *args, **kwargs): self = super(MySubclass, cls).__new__(cls, *args, **kwargs) // Do __init...

type of class in python

why if I do: class C(): pass type(C()) I got: <type 'instance'>, but if I do: class C(object): pass type(c()) I got: <class '__main__.c'> ? The first is not very userfull ...