0% found this document useful (0 votes)
126 views11 pages

Understanding Constructors in OOP

Constructors are special methods in object-oriented programming that are automatically called when an object is created. The constructor method in Python is defined using the __init__() method. The __init__() method initializes the attributes of the newly created object and can take arguments to set the initial state of the object. Default arguments allow constructors to have optional parameters to provide default values when objects are created without specifying all arguments.

Uploaded by

Fee21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views11 pages

Understanding Constructors in OOP

Constructors are special methods in object-oriented programming that are automatically called when an object is created. The constructor method in Python is defined using the __init__() method. The __init__() method initializes the attributes of the newly created object and can take arguments to set the initial state of the object. Default arguments allow constructors to have optional parameters to provide default values when objects are created without specifying all arguments.

Uploaded by

Fee21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Object-Oriented

Programming

Constructors

Instructor : Umid Suleymanov


Constructors
• Member function that is automatically called when an object
is created

• Purpose is to construct an object

• Constructor function name is __init__

• Has no return type

• Always take self as first argument


Making it simpler
• What if we could call our class as if it class Point:
    
were a real function, with inputs?     def __init__(self, x, y):
• Then we could pass the attributes of         self.move(x, y)
        
objects as inputs.     def move(self, x, y):
        self.x = x
        self.y = y
       
• We can do this by defining what is called         
# Constructing a Point
constructor. point = Point(3, 5)
• A method that builds your object for print(point.x, point.y)

you.

3
Making instances more flexibly

• To create new instances with inputs, we must class Point:


    
define a function named __init__     def __init__(self, x, y):
• It’s the predefined name for a method that         self.move(x, y)
        
initializes new objects.     def move(self, x, y):
        self.x = x
        self.y = y
• Our __init__ function will take three inputs:        
        
• self, because all methods take that. # Constructing a Point
• And a x coordinate and y coordinate. point = Point(3, 5)
print(point.x, point.y)

4
Our whole Point class
class Point:
    
    def __init__(self, x, y):
        self.move(x, y)
        
    def move(self, x, y):
        self.x = x
        self.y = y
        
    def reset(self):
        self.move(0, 0)

        
# Constructing a Point
point = Point(3, 5)
print(point.x, point.y)

5
Default Constructors
• A default constructor is a constructor that takes no
arguments.

• If you write a class with no constructor at all, Python will


write a default constructor for you, one that does nothing.

• A simple instantiation of a class (with no arguments) calls the


default constructor:
point = Point()
Passing Arguments to Constructors
• To create a constructor that takes arguments:
• indicate parameters in prototype:

    def __init__(self, x, y):
        self.move(x, y)

• Use parameters upon creating instance:


point = Point(3, 5)
Passing Arguments to
Constructors
• You can pass arguments to the constructor when you create
an object:

point = Point(10, 5);


Default Arguments
• Now, our point can never go without a y coordinate!
• If we try to construct a point without including the proper
initialization parameters, it will fail with a not enough
arguments error:
Default Arguments
• What if we don't want to make those two arguments
required?
• Well then we can use default arguments.
• The keyword argument syntax appends an equals sign
after each variable name.

class Point:
     def __init__(self, x=0, y=0):
         self.x = x
self.y = y
Default Arguments
• If the calling object does not provide that argument, then
the default argument is used instead
class Point:
     def __init__(self, x=0, y=0):
         self.x = x
self.y = y

#We call Point without any arguments
point = Point()
#point now has x and y equal to 0

You might also like