__author__ = "Narendra Boyina"
# -----------------------------------------------------------------------------
# Copyright (c) 2025 BR Technologies PVT LTD
# -----------------------------------------------------------------------------
"""
Topics to be covered in today's class:
--> Introduction of oops concept
--> Class syntax
--> Overview of Terminology
"""
"""
To understand the oops concept, we have to understand following concepts
1. what is class ?
Ans: Class is nothing but just blue-print.There is no use of class definition,
until unless you have created the instance/object for the particular class.
2. Difference between method and function ?
Ans: Function which is defined inside of the class is a method.
Function which is defined outside of the class is a Function.
3. What is Constructor method (def __init__(self)) ?
Ans: Constructor method (def __init__(self)) is a special kind of method,
as soon as you have created an object/ instance for the class then
automatically ___init__(self) method will be executed, without calling it.
how many no. of instances/objects you have created for a specific class
those many times __init__(self) will be executed without calling it
4. How many instances/objects we can create for single class?
N number of instances/ objects we can create for a single class.
5. What are the extra advantages of classes compared with functions?
Ans: 1. Grouping of function in the form of methods, provided with specific class name.
2. We can inherit the class into another class
3. We can perform multiple class related properties (Data hiding/abstraction/ method overriding/encapsulation...)
Syntax: class syntax also similar to function syntax
def fun_name([optional argument]):
['''doc_string''']
function_suite
class class_name:
['''doc_string''']
class suite
"""
"""
Overview of Terminology :
--> Variables --> We will declare variables outside of the function & outside of the class
--> arguments --> the variables declared inside the function definition called as arguments
--> attributes --> the variables declared inside the class definition called as attributes
--> class attributes are clasified into 2 types (1. class variables 2. instance variables)
--> By using dot notation, we can access Class attributes (class variables and instance variables) and methods.
But there is big difference between (class variables and instance variables)
"""
"""
Class variable − A variable that is shared by all instances of a class ((usually declared at the top))
Class variables are defined within a class but outside any of the class's methods.
Class variables are not used as frequently as instance variables are.
Data member/ instance variable/object:− Instance variable that holds data associated with a class and its objects.
==================================================================================================================
Object − A unique instance of a data structure that is defined by its class.
An object comprises both attributes (class variables & instance variables) and methods.
"""
# company = "BR Techno Solutions" # company is a variable / global variable
# year = 2019 # age is a variable / global variable
def suresh(company, year): # company, year are arguments
print(company, year)
class Employee:
company = "BR Techno Solutions" # company is attribute / class variable
year = 2019 # company is attribute / class variable
def __init__(self, emp_name, employee_id): # emp_name, employee_id are attributes or instance variables
self.emp_name = emp_name
self.employee_id = employee_id
print("This was printed , because of you have created object for this class")
print("Name is {0} and employee_id is {1}".format(emp_name, employee_id))
def suresh(a, b, c): # a b, c are class attributes/ instance variables
print((a + b) * c)
def swetha(self, x=10):
print("Maha")
name = "Narendra" # variable/ global variable
number = "HM0002162" # variable/ global variable
suresh_obj = Employee(name, number) # creating object
ganesh_obj = Employee("ganesh", 23234) # creating object
# v_reddy_obj = Employee("venkatesh", 893234) # creating object
# print(id(suresh_obj),"\n",id(ganesh_obj),"\n", id(v_reddy_obj))
"""Accessing the attributes with the help of object and printing them outside of the class definition"""
# print(suresh_obj.emp_name) # calling class attributes (instance variable) from outside of class using object
# print(suresh_obj.employee_id) # calling class attributes(instance variable) from outside of class using object
# print(suresh_obj.company) # calling class attributes (class variable) from outside of class using object
# print(suresh_obj.year) # calling class attributes (class variable) from outside of class using object
""" we can modifiy the attribute's values from outside of the class by using object"""
# suresh_obj.emp_name = "Sai srinivas" # changing class attribute
# print(suresh_obj.emp_name)
# suresh_obj.emp_name = "venkat" # changing class attribute's value
# print(suresh_obj.emp_name) # changing class attribute's value
# suresh_obj.year = 2024 # changing class attribute's value
# print(suresh_obj.year)
""" We can add new attributes to the class from outside, by using Object"""
suresh_obj.salary = 88500 # we can add attribute (instance variable) from outside of class using object
suresh_obj.designation = "Team Lead" # we can add attribute (instance variable) from outside of class using object
print(suresh_obj.emp_name, suresh_obj.employee_id, suresh_obj.salary, suresh_obj.designation)
""" By using object, we can modify the attribute values """
suresh_obj.salary = 106000
print(suresh_obj.emp_name, suresh_obj.employee_id, suresh_obj.salary, suresh_obj.designation)
""" Creating a different object for the same class"""
naresh_obj = Employee("Naresh", "345678")
print(naresh_obj.emp_name)
print(naresh_obj.emp_name, naresh_obj.employee_id, naresh_obj.company, naresh_obj.year)
""" By using object, modifying existing attribute's value"""
naresh_obj.year = 2025
print(naresh_obj.year)
""" adding new attribute to 2nd object from outside of the class definition"""
naresh_obj.place = "Bangalore"
print(naresh_obj.place)
""" Creating different object for the same class"""
# meera_obj = Employee("Meera","HM530022")
# print(meera_obj.emp_name, meera_obj.employee_id,meera_obj.year,meera_obj.company)
""" observing memory locations for each object"""
# print(id(suresh_obj), id(naresh_obj), id(meera_obj))
# print(id(suresh_obj) != id(naresh_obj) != id(meera_obj) )
"""
Result:
=======
This was printed , because of you have created object for this class
Name is Narendra and Number is HM0002162
Narendra
HM0002162
Durga
Prasanth
Prasanth HM0002162 88500 Test Module Lead
after created an object, we can also access variables with that object
Ex:
a.name
a.number
"""Author: Boyina Narendra
Supporting Author: M. Meera Sindhu
Request: If you find this information useful, please provide your valuable comments
No comments:
Post a Comment