w3resource

Python: Create an instance of a specified class and display the namespace of the said instance


3. Create an instance of a class and display its namespace

Write a Python program to create an instance of a specified class and display the namespace of the said instance.

Sample Solution:

Python Code:

class Student: 
    def __init__(self, student_id, student_name, class_name):
        self.student_id = student_id
        self.student_name = student_name
        self.class_name = class_name 
student = Student('V12', 'Frank Gibson', 'V')
print(student.__dict__)

Sample Output:

{'student_id': 'V12', 'student_name': 'Frank Gibson', 'class_name': 'V'}

Flowchart:

Flowchart: Create an instance of a specified class and display the namespace of the said instance

For more Practice: Solve these Related Problems:

  • Write a Python program to instantiate a previously defined class and print the instance’s __dict__ to show its attributes.
  • Write a Python program to create an instance of a class with an __init__ method and display its namespace including default attributes.
  • Write a Python program to dynamically add attributes to an instance and then print its __dict__ to verify the changes.
  • Write a Python program to instantiate a class, modify some attributes, and then compare the instance’s __dict__ before and after modification.

Go to:


Previous: Write a Python program to create a class and display the namespace of the said class.
Next: Write a python program which import the abs() function using the builtins module, display the documentation of abs() function and find the absolute value of -155.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.