Powered by Ajaxy
Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Python: Create a custom class

class Employee:
    def __init__(self, fullName, salary):
        self.fullName = fullName
        self.salary  = salary
    def getFullName(self):

Python: Documenting a Class

class Animation: #définition d'une classe animation
  # My documentation starts here
    2D and 3D animation 
    in python
  # End d

Python: How to use dictionaries as constructor argument

class Animation(object):
    def __init__(self, **points):
        self.__dict__ = points
p = Animation(x=3.4, y=6.8)
print p.__module__
print p.__

Python: using a class instance as a function

class Memory:
    def __call__(self, *args):
        for elem in range(len(args)):
            print args[elem]
print obj()
print obj(

Python: Reading classes documentation

# print documentation of Exception class
print Exception.__doc__
# print documentation of ZeroDivisionError class
print ZeroDivisionError.__doc__

Python: Create a class with a constructor.

class Student:
     # Construtor
     def __init__ (self, name, lastname, age):
         self.name   = name
         self.lastname = lastname

Python: Creating Instance Variables

class Animation: 
   Class documentation
anim=Animation()  # create an objet
# Define two instances variables

Python: Calculate percentages

def getPercent(a, b, c):
    def pc(x): return (x*100.0) / (a+b+c)
    print "Percentages are:", pc(a), pc(b), pc(c)
getPercent(15, 9, 11);

Python: Define a constructor

class Pyramide:
     # Class constructor
     def __init__(self, x, y):
         self.point_x = x
         self.point_y = y
obj = Pyramide(3.0, -4.

Python: Define a static method

class MaClasse(object):
    def mtStatic(msg):
     print msg
    mtStatic = staticmethod(mtStatic)
obj = MaClasse( )
MaClasse.mtStatic("Welcom to

Python: create a class constructor

class Person(object):
    def __init__(self):
        print "A new person has been created!"
    def Print(self):
        print "I'm an instance of

Python: create class instances

class Animation: # define a class
   class documentation
# Create an instance of class 'Animation'
print type(ani

Python: calculate message signatures using MD5 module

import md5
hash = md5.new()
hash.update("you, me, java, and pyton")
print repr(hash.digest())

Python: Accessing Built-In Class Attributes

class Animation: # define a class
print Animation.__dict__    #Dictionary containing the class's namespace
print Animation.__doc__     #Cla

Python: define a subclass

class A:
class B:
class C(A): # C is subclass of A
class D(A,B,C): # D is subclass of A, B, and C