# Init method is used to declare variables to the main method
# Self is for after initialising variables self is used to use that variables in another menthods
class Mobile:
def __init__(self,brand,battery,ram,camera,price):
self.brand=brand
self.battery=battery
self.ram=ram
self.camera=camera
self.price=price
def display(self):
print("Brand is :",self.brand)
print("Battery is :",self.battery)
print("Ram is :",self.ram)
print("Camera is :",self.camera)
print("Price is :",self.price)
print("-------------------")
obj=Mobile('apple','400mah','8gb','84mp','40000')
obj.display()
obj2=Mobile('Motorola','400mah','8gb','84mp','40000')
obj2.display()
# Difference between Class and a function
Defining a Function
def add_numbers(x, y):
return x + y
--------------------------------------------------------------
Defining a Class
above function we can also write in the format of class.
class Adder:
def __init__(self, x, y):
self.x = x
self.y = y
def add_numbers(self):
return self.x + self.y
def __init__(self, x, y):
self.x = x
self.y = y
def add_numbers(self):
return self.x + self.y
###################################################################################
The __init__ method initializes the instance variables self.x and self.y with the values passed when creating an object of the class.
Calling function and Class
# Using the original function
result_function = add_numbers(3, 4)
print("Result using function:", result_function)
# Using the class
adder_instance = Adder(3, 4)
result_class = adder_instance.add_numbers()
print("Result using class:", result_class)
__init__() method is used to declare the Global variables . Whenever we initialize a instance Object then global variables will get assigned with those values .
The variables which are declared inside the methods are called local varialbes
Global Variable --- Scope is Global
Instance Variables -- Scope is for that particular Class
Local Variables --- Scope is for a particular method or Function .
1)Package ---> Module(fileName) ----> Method or functions(Methods or Funtions),variables
2)from mainpackage(create__init__.py).subpackage,module.(create__init__.py).module(fileName)
import variable,method
module.Function()
module.variable
here mainpackage is Folder In this - Folder create __init__.py
here subpackage is also a Folder - In this Folder create __init__.py
__init__ ----- These __ variables are built in functions and Built in Variables .
##################################################################################
Dunder or Special Methods
---------------------------------
A functional difference between special methods and common methods is that special methods can be called indirectly when we use a special syntax (such as arithmetic operations, subscripting, and slicing).
##################################################################################
Static Method:
A static method is a method that belongs to a class rather than an instance of the class. This means that a static method can be called on the class itself, without the need to create an object of the class.
Class method:
A class method is a method that belongs to a class rather than an instance of the class, but unlike a static method, it can access and modify the class variables.
Instance method:
Instance methods are used for operations that require object-specific data.
class Person:
"""
A simple class representing a person.
"""
# Static method to validate a person's age
@staticmethod
def is_valid_age(age):
return age >= 0 and age <= 120
# Class method to create a new person object with default values
@classmethod
def new_person(cls, name="John Doe", age=25):
return cls(name, age)
def __init__(self, name, age):
self.name = name
self.age = age
# Instance method to introduce the person
def introduce(self):
return f"Hi, I'm {self.name} and I'm {self.age} years old."
# Usage example
person1 = Person.new_person("Alice", 30)
person2 = Person("Bob", 45)
# Static method usage
print(f"Is 20 a valid age? {Person.is_valid_age(20)}")
print(f"Is -10 a valid age? {Person.is_valid_age(-10)}")
# Instance method usage
print(person1.introduce())
print(person2.introduce())
The @staticmethod and @classmethod decorators are powerful features in Python that allow us to define methods that are associated with a class rather than an instance of the class.
##################################################################################
No comments:
Post a Comment