self and init in python
class Computer:
def __init__(self):
self.name= ("Pankaj")
self.age= 28
c1=Computer()
c2=Computer()
c1.name= "Garg"
print(c1.name)
print(c2.name)
self and init in python
class Computer:
def __init__(self):
self.name= ("Pankaj")
self.age= 28
c1=Computer()
c2=Computer()
c1.name= "Garg"
print(c1.name)
print(c2.name)
__init__.py file in python
# __init__.py
# you can comment here about your package.
# you can also add external libraries to your package.
# for example:
import numpy
import pandas
# you can also add your package information in this file.
__version__ = '2.1.1'
__author__ = 'Depressed Dingo'
# __all__ make others able to access to this datas by importing our package.
__all__ = ['numpy', 'pandas', '__version__', '__author__']
# -----------------------------------------
# main.py
# example of using this.
from test_package import *
# or
from test_package import numpy
print(__auther__) # --> Depressed Dingo
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
# breadth = 120 units, length = 160 units, 1 sq unit cost = Rs 2000
r = Rectangle(160, 120, 2000)
print("Area of Rectangle: %s sq units" % (r.get_area()))
__init__ python
class A(object):
def __init__(self):
self.x = 'Hello'
def method_a(self, foo):
print self.x + ' ' + foo
example of __init__ self
class MyClass(object):
i = 123
def __init__(self):
self.i = 345
a = MyClass()
print(a.i)
print(MyClass.i)
# Output:
# 345
# 123
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us