Error:

Define class

class MyClass(object):
    def __init__(self):
        print "MyClass"

class AnotherClass(MyClass):
    def __init__(self):
        print "AnotherClass"

Call parent's constructor

class MyClass(object):
    def __init__(self):
        print "MyClass"

class AnotherClass(MyClass):
    def __init__(self):
        super(AnotherClass, self).__init__()
        print "AnotherClass"

obj = AnotherClass()

Define class variable

class MyClass(object):
    class_var = 42

    def __init__(self):
        self.instance_var = 43

Define instance variable

class MyClass(object):
    def __init__(self):
        self.instance_var = 42

Define class method

class MyClass(object):
    class_var = 42
    def __init__(self):
        self.instance_var = 43

    @classmethod
    def method(klass):
        print klass.class_var

MyClass.method() # 42

Define instance method

class MyClass(object):
    def __init__(self):
        self.instance_var = 43

    def method(self):
        print self.instance_var

obj = MyClass()
obj.method() # 43

Get class of the object

class MyClass(object):
    def __init__(self):
        self.data = []

obj = MyClass()
class_name = obj.__class__.__name__ # 'MyClass'

List attributes of the object

class MyClass(object):
    MYVAR = 42
    def __init__(self):
        self.data = []

    def method1(self):
        pass

obj = MyClass()
for name in dir(obj):
    attr = getattr(obj, name)
    print '%s: %s' % (name, attr.__class__.__name__)

List methods of the object

class MyClass(object):
    MYVAR = 42
    def __init__(self):
        self.data = []

    def method1(self):
        pass


obj = MyClass()
methods = [method for method in dir(obj) if callable(getattr(obj, method))]

Getters and setters for instance property

class MyClass(object):
    def __init__(self):
        self._id = 0

    @property
    def id(self):
        print "getter"
        return self._id

    @id.setter
    def id(self, newid):
        print "setter"
        self._id = newid

obj = MyClass()
obj.id = 10 # setter
print obj.id # getter

Check if instance has method

def has_method(o, method):
    return hasattr(o, method) and callable(getattr(o, method))

class MyClass(object):
    def __init__(self):
        self.data = []

    def method1(self):
        pass


obj = MyClass()
print has_method(obj, 'method1') # True
print has_method(obj, 'method2') # False

List all variables for instance

def get_vars(cls):
    return [name for name, obj in cls.__dict__.iteritems()
                if not name.startswith("__") and not callable(obj)]

class MyClass(object):
    CLASS_VAR = 42
    def __init__(self):
        self.data = []

    def method1(self):
        pass

print get_vars(MyClass) # ['CLASS_VAR']

List all variables for class

>>> def get_vars(cls):
...     return [name for name, obj in cls.__dict__.iteritems()
                if not name.startswith("__") and not inspect.isroutine(obj)]
>>> get_vars(cols)

Check whether a class is a subclass of another class

class MyClass(object):
    def __init__(self):
        pass

class AnotherClass(MyClass):
    def __init__(self):
        pass

issubclass(AnotherClass, MyClass) # True