Unit 2 Final
Unit 2 Final
Here,
➢ Bike - the name of the class
➢ name/gear - variables inside the class with default values "" and 0 respectively.
➢ The variables inside a class are called attributes
Some points on Python class:
•Classes are created by keyword class.
•Attributes are the variables that belong to a class.
•Attributes are always public and can be accessed using the dot (.) operator. Eg.:
Myclass.Myattribute
2/20/2025 K.ABINAYA Assistant Professor Sbu 3
Python Objects
➢ An object is called an instance of a class. For example, suppose Bike is a class then
we can create objects like bike1, bike2, etc from the class.
➢ Here's the syntax to create an object.
➢ Here, bike1 is the object of the class. Now, we can use this object to access the class
attributes.
2/20/2025 K.ABINAYA Assistant Professor Sbu 4
Access Class Attributes Using Objects
➢ We use the . notation to access the attributes of a class. For example,
➢ Here, we have used bike1.name and bike1.gear to change and access the value of
name and gear attribute respectively.
➢ Standard library functions - These are built-in functions in Python that are available to use.
➢ User-defined functions - We can create our own functions based on our requirements.
Python Function Declaration
Here,
def - keyword used to declare a function
function_name - any name given to the function
arguments - any value passed to function
return (optional) - returns value from a function
2/20/2025 K.ABINAYA Assistant Professor Sbu 11
Example: Python Function
If we create a function with arguments, we need to pass the corresponding values while
calling them. For example,
The return statement also denotes that the function has ended. Any code after
return is not executed.
In the above example, we have created a function named find_square(). The function
accepts a number and returns the square of the number.
2/20/2025 K.ABINAYA Assistant Professor Sbu 16
Example 3: Add Two Numbers
1. Code Readability - Functions help us break our code into chunks to make our
program readable and easy to understand.
➢ Here, we have used the . notation to call the method. Finally, the statement inside the
method is executed.
Where,
➢ def: The keyword is used to define function.
➢ __init__() Method: It is a reserved method. This method gets called as soon as an object of a
class is instantiated.
➢ self: The first argument self refers to the current object. It binds the instance to the __init__()
method. It’s usually named self to follow the naming convention.
Note: The __init__() method arguments are optional. We can define a constructor with any number
of arguments.
➢ In the above code, the object st called the second constructor whereas both have the
same configuration.
➢ The first method is not accessible by the st object. Internally, the object of the class
will always call the last constructor if the class has multiple constructors.
SN Function Description
1 getattr(obj,name,default) It is used to access the attribute of the
object.
2 setattr(obj, name,value) It is used to set a particular value to the
specific attribute of an object.
➢ we can see that a single operator + has been used to carry out different operations for
distinct data types. This is one of the most simple occurrences of polymorphism in
Python.
Method Overriding
• Method overriding is an ability of any object-oriented programming language that
allows a subclass or child class to provide a specific implementation of a method
that is already provided by one of its super-classes or parent classes.
• When a method in a subclass has the same name, same parameters or signature and
same return type(or sub-type) as a method in its super-class, then the method in the
subclass is said to override the method in the super-class.
1. Same name methods but have 1. Same name methods but have
different signatures eg. Sum(a+b) same signatures
Sum(a+b+c)
➢ ABC is defined in a way that the abstract methods in the base class are created by
decorating with the @abstractmethod keyword and the concrete methods are
registered as implementations of the base class.