Warning: session_start(): open(/tmp/sess_4ec7a0234c200b9ef53ab4dcdc060ae0, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Add Attributes To An Object In Python - LearnShareIT

How To Add Attributes To An Object In Python

In this article, we will show you how to add attributes to an Object in Python. You usually do that when you forget to set attributes when creating an Object or you want to add attributes to a class without needing to know it.

Add Attributes To An Object In Python

Normally, you usually create attributes in the __init__() function of the original class. So when you create a new object in the main function, attributes will be created based on the __init__() function of this object. Sometimes, you want to add attributes to an object after creating it. To do that, you can use a dot or the setattr() function in Python.

Add an attribute to an Object with a dot

In this way, after creating the object, you simply use a dot to add attributes to this object by the following command.

nameObj.nameAttr = value

Parameters

  • nameObj: The name of the object.
  • nameAttr: The name of the new attribute.
  • value: The value of the new attribute.

Look at the example below to learn more about this solution.

class Student:
    def __init__(self, name='', id=''):
        # Student() has two attributes: name and id
        self.name = name
        self.id = id

    def __str__(self):
        return self.name + ' ' + self.id

# Create the new object
student = Student('Thomas Valen', 'PTIT606')
print(student)

# Add the attribute named 'university' with the value 'PTIT' to this student
student.university = 'PTIT'
print(student.university)

Output

Thomas Valen PTIT606
PTIT

Add an attribute to an Object with the setattr() function

Besides a dot, you can also use the setattr() function to add attributes to an Object in Python.

Syntax

setattr(object, name, value)

Parameters

  • object: The name of the object.
  • name: The name of the new attribute.
  • value: The value of the new attribute.

Look at the example below to learn more about this solution.

class Student:
    def __init__(self, name='', id=''):
        # Student() has two attributes: name and id
        self.name = name
        self.id = id

    def __str__(self):
        return self.name + ' ' + self.id

# Create the new object
student = Student('Thomas Valen', 'PTIT606')
print(student)

# Add the attribute named 'university' with the value 'PTIT' to this student
setattr(student, 'university', 'PTIT')
print(student.university)

Output

Thomas Valen PTIT606
PTIT

Add multiple attributes to an Object with the setattr() function

You can add multiple attributes to an Object with the setattr() function.

Look at the example below.

class Student:
    def __init__(self, name='', id=''):
        # Student() has two attributes: name and id
        self.name = name
        self.id = id

    def __str__(self):
        return self.name + ' ' + self.id

# Create the new object
student = Student('Thomas Valen', 'PTIT606')
print(student)

subject = ['python', 'java', 'r']

# Add attributes as all elements in the 'subject' list
for element in subject:
    setattr(student, element, 'A+')

print(vars(student))

Output

Thomas Valen PTIT606
{'name': 'Thomas Valen', 'id': 'PTIT606', 'python': 'A+', 'java': 'A+', 'r': 'A+'}

Besides, click here if you want to check all the existing attributes in an Object.

Summary

We have shared with you how to add attributes to an Object in Python in two ways. To do that, you can use a dot or the setattr() function in Python. Note that the name of the attribute of the setattr function must be surrounded by single quotes. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *