0% found this document useful (0 votes)
33 views7 pages

Passing Instance Attribute Values in Constructor

The document discusses passing instance attribute values in constructors in Python. It explains that constructor parameters define instance attributes and their values. Default values can also be set for parameters. The document also compares class attributes, which are shared across all instances, versus instance attributes, which are specific to each object instance.

Uploaded by

rahul
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)
33 views7 pages

Passing Instance Attribute Values in Constructor

The document discusses passing instance attribute values in constructors in Python. It explains that constructor parameters define instance attributes and their values. Default values can also be set for parameters. The document also compares class attributes, which are shared across all instances, versus instance attributes, which are specific to each object instance.

Uploaded by

rahul
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/ 7

Passing Instance Attribute Values in

Constructor
• You can specify the values of instance attributes through the constructor. The following
constructor includes the name and age parameters, other than the self parameter.
• class Student:
• def __init__(self, name, age):
• self.name = name
• self.age = age
• Now, you can specify the values while creating an instance, as shown below.
• >>> std = Student('Bill',25)
• >>> std.name
• 'Bill'
• >>> std.age
• 25
• Note:
• You don't have to specify the value of the self parameter. It will be
assigned internally in Python.
• You can also set default values to the instance attributes. The
following code sets the default values of the constructor parameters.
So, if the values are not provided when creating an object, the values
will be assigned latter.
Example: Instance Attribute Default Value
• class Student:
• def __init__(self, name="Guest", age=25)
• self.name=name
• self.age=age
• Now, you can create an object with default values, as shown below.
• >>> std = Student()
• >>> std.name
• 'Guest'
• >>> std.age
• 25
Class Attributes vs Instance Attributes in Python

• Class Attributes vs Instance Attributes in Python


• Python By TutorialsTeacher 31 Jan 2021
• Class attributes are the variables defined directly in the class that are shared
by all objects of the class.

• Instance attributes are attributes or properties attached to an instance of a


class. Instance attributes are defined in the constructor.

• The following table lists the difference between class attribute and instance
attribute:
Class Attributes vs Instance Attributes in Python
lass Attribute Instance Attribute
Defined directly inside a class. Defined inside a constructor using
the self parameter.
Shared across all objects. Specific to object.
Accessed using class name as well as Accessed using object dot notation
using object with dot notation, e.g. object.instance_attribute
e.g. classname.class_attribute or object
.class_attribute

Changing value by Changing value of instance attribute


using classname.class_attribute = will not be reflected to other objects.
value will be reflected to all the
objects.
use of class attribute
• The following example demonstrates the use of class attribute count.
• class Student:
• count = 0
• def __init__(self):
• Student.count += 1
• In the above example, count is an attribute in the Student class.
Whenever a new object is created, the value of count is incremented
by 1. You can now access the count attribute after creating the
objects, as shown below.
use of class attribute
• >>> std1=Student()
• >>> Student.count
•1
• >>> std2 = Student()
• >>> Student.count
•2

You might also like