User Defined Classes N Objects
User Defined Classes N Objects
AND OBJECTS
DEEPAK H G
ASST. PROF. CSE
INTRODUCTION
• Procedure-oriented programming
• Program is written using functions or blocks of statements to manipulate data.
• Classes and objects are the two main aspects of object oriented programming.
• A class creates a new type and object is an instance of the class.
• A class provides a blueprint or template, using which objects are created.
• In Python, everything is an object or an instance of some class. For example, all integer
variables are instances of class int and all string variables are instances of class str.
DEFINING A CLASS
• A class is created with the keyword class followed by the class name.
• Within the indentation of the class block, write the class members i.e., attributes and
actions.
• The attributes are represented by variables and actions are performed by methods.
• Class members are accessed through class objects.
DEFINING A CLASS
• Naming Convention
• Each word of a class name should start with a capital letter.
• When a class represents exception, then its name should end with the word 'error'.
DEFINING A CLASS
• Note:
• Class definition can appear anywhere in the program.
• Usually class definition is written near the beginning of the program, after the import
statement.
• A class creates a new local namespace where all its members are defined.
• A class that has no statements should have a pass statement at least.
CREATING OBJECTS
• Once a class is created, the next job is to create an object or instance of that class.
• The class members are accessed through the class object using the dot (.) operator.
• When an instance of the class is created, the instance name holds the memory address of the instance.
• Note: The above result shows that an instance was built from Person class and the instance is stored in memory location.
CREATING MULTIPLE INSTANCES
• Note: Although the two instances are created from the same class, they are stored as separate entities within the
program.
EXERCISE
• Create a class called "Animals", and create 2 instances from it, namely, "lion" and "tiger".
DATA ABSTRACTION
• Data abstraction is a process by which data and functions are defined in such a way that
only essential details are provided to the outside world and the implementation details
are hidden.
• In Python, a class provides methods to the outside world to provide the functionality of
the object or to manipulate the object's data.
DATA ENCAPSULATION
• Encapsulation involves the bundling of data members and functions inside a single class.
• Encapsulation defines different access levels for data variables and member functions of the
class:
• public
• Any data or function with access level public can be accessed by any function belonging to any class.
• private
• Any data or function with access level private can be accessed only by the class members, in which it is
declared.
• In Python, private variables are prefixed with a double underscore.
• Example: __date_of_birth = None
METHOD
• Objects are associated with certain attributes and actions with them.
• Example:
• Consider a car. It has attributes like make, model, variant, color, number of wheels, etc.
• It also perform actions like accelerate, stop, turn, etc.
• self parameter contains the memory address of the instance of the class (object).
• We use self variable to refer all the instance variables and instance methods.
• When an instance of the class is created, the instance name holds the memory address
of the instance. This memory address is internally passed to self.
• Example:
• Think about a cricket team we’ve never seen their play before. How do we distinguish each
player from the next? Probably we use the numbers on the back of the team member's
jerseys.
SELF PARAMETER
• Note:
• The self argument is ignored while the init method is called. The init method is called immediately when an object is
created.
• It is recommended to initialize all the attributes of a class in the __init__() method.
DESTRUCTOR
• Note:
• If the method is intended to be accessed through instance, use self as the first parameter in the definition. Without the self
parameter, the method can only be accessed by the class itself.
• A class can have as many methods as required.
VARIABLES
• Naming Convention:
• Variable names should be all lower case letters.
• When multiple words are used, separate them using an underscore.
CLASS OR STATIC VARIABLES
• Class variables are the variables whose single copy is available to all the instances of the
class.
• Class variables are also called static variables.
• Class variables are accessed outside the class using the class name followed by the dot
operator.
CLASS OR STATIC VARIABLES
• Class variables are usually used to keep a count of number of objects created from a
class.
• Class variables are used to define constants associated with a particular class or provide
default attribute values.
INSTANCE VARIABLES
• Instance variables are the variables whose separate copy is created in every instance.
• Instance variables are defined and initialized using the constructor.
• Instance variables are accessed using instance methods.
EXERCISES
• 1. Create a Dog class that has one class attribute and two instance level attributes. The
class attribute should be “species” with a value of “Canine.” The two instance attributes
should be “name” and “breed.” Then instantiate two dog objects, a Husky named Sammi
and a Chocolate Lab named Casey.
• 2. Create a Person class that has a single instance level attribute of “name.” Ask the user
to input their name, and create an instance of the Person class with the name they typed
in. Then print out their name.
PUBLIC AND PRIVATE DATA MEMBERS
• Public members can be accessed from anywhere in the program i.e., they can be
accessed within the class as well as from outside the class in which they are defined.
• Private members can only be accessed within the class.
• Private members are defined in the class with a double underscore prefix.
PUBLIC AND PRIVATE DATA MEMBERS
PUBLIC AND PRIVATE DATA MEMBERS
PRIVATE METHODS
• A private method can be accessed using the object name or the class name from outside the class only if necessary.
CLASS METHODS
• Class methods are called by a class and not by instance of the class.
• The first parameter of the class methods is cls and not self.
STATIC METHODS
• A functionality that belongs to a class, but does not require the object, is placed in the static method.
• Static methods does not receive any additional arguments like self, cls.
• A static method is defined using a built-in function staticmethod.
• Static method knows nothing about the class and just deals with the parameters.
• Static method cannot access the properties of the class itself.
• When we need a utility function that doesn't access any properties of a class but makes sense that it
belongs to the class, we use static methods.
• A static method can be called either on the class or on an instance.
STATIC METHODS