Wednesday, August 12, 2015

class and object in python

https://www.jeffknupp.com/blog/2014/06/18/improve-your-python-python-classes-and-object-oriented-programming/ 
 
http://stackoverflow.com/questions/1593441/constructors-in-python
 
http://stackoverflow.com/questions/3691101/what-is-the-purpose-of-str-and-repr-in-python
 
http://stackoverflow.com/questions/3694371/how-do-i-initialize-the-base-super-class-in-python
 
#Super class 
A superclass is a class that has been extended by another class. It 
allows the extending class to inherit its state and behaviors. 
 
Note: 
1. self = this (self is equivalent to this of C#, C++) = representation
   of specific instance of the class.  
2. self is must for all method definition except for @staticmethod 
3. Object: Object is an instance of a class 
#define class attribute: self.attribute1 = 0
self.attribute1 = 0
self.attribute2 = "Hello"  
 
Simple Example-1 
 
#---This is the class part---#
class Arit:
    def add(self,a,b):
        return a+b
    def subs(self,a,b):
        return a-b


#---This is the part that uses the class---#
n1=10
n2=4

operation=Arit()

print "The addition is", operation.add(n1,n2)

print "The substraction is", operation.subs(n1,n2)
 
 
OR
 
#---This is the class part---#
class Arit():
    def add(self,a,b):
        return a+b
    def subs(self,a,b):
        return a-b


#---This is the part that uses the class---#
n1=10
n2=4

operation=Arit()

print "The addition is", operation.add(n1,n2)

print "The substraction is", operation.subs(n1,n2)  
 
 
Simple Example-2:
 
class SimpleClass:
    def __init__(self):
        self.cumul=0
    def accumulate(self,amount):
        self.cumul = self.cumul + amount

simpleObject = SimpleClass()
print simpleObject.cumul
simpleObject.accumulate(10)
print simpleObject.cumul  
 
#
class MyClass(object):
  def __init__(self, x, y, angle):
    self.x = x
    self.y = y
    self.angle = angle
The constructor is always written as a function called __init__(). It must always take as its first argument a reference to the instance being constructed. This is typically called self. The rest of the arguments are up to the programmer.
The object on the first line is the superclass, i.e. this says that MyClass is a subclass of object. This is normal for Python class definitions.
You access fields (members) of the instance using the self. syntax.

# prints during initialization
class Foobar():
    """This will create Foobar type object."""

    def __init__(self):
        print "Foobar object is created."

    def __repr__(self):
        return "Type what do you want to see here."

a = Foobar()

print a 
 
# Static method:
class Foobar():
    """This will create Foobar type object."""
    @staticmethod
    def a():
        print "Foobar object is created."

Foobar.a()
 
# Using print in class
class Foobar():
    """This will create Foobar type object."""
    def a(self):
        print "Foobar object is created."
b = Foobar()
b.a()
 
#Constructor with argument:
class Num:
    def __init__(self,num):
        self.n = num
    def getn(self):
        return self.n
    def getone(self):
        return 1
myObj = Num(3)

print myObj.getn()
  
 
# Attributes (global and instance specific attribute:
 
class Car(object):

    wheels = 4

    def __init__(self, make, model):
        self.make = make
        self.model = model

mustang = Car('Ford', 'Mustang')
print mustang.wheels
# 4
print Car.wheels
# 4
  

No comments:

Post a Comment