python init.py code example
Example 1: __init__ python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
p1 = Person("John", 36) // Object definition
print(p1.name)
print(p1.age)
Example 2: init function in python
class Rectangle:
def __init__(self, length, breadth, unit_cost=0):
self.length = length
self.breadth = breadth
self.unit_cost = unit_cost
def get_area(self):
return self.length * self.breadth
def calculate_cost(self):
area = self.get_area()
return area * self.unit_cost
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s sq units" % (r.get_area()))
Example 3: python module initialization
import psutil
Example 4: __init__ python
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
Example 5: import __init__.py
from . import *
from . import hello_world