w3resource

Python: Create a class and display the namespace of the said class


2. Create a Class and Display Its Namespace

Write a Python program to create a class and display the namespace of that class.

Sample Solution:

Python Code:

class py_solution:
    def sub_sets(self, sset):
        return self.subsetsRecur([], sorted(sset))    
    def subsetsRecur(self, current, sset):
        if sset:
            return self.subsetsRecur(current, sset[1:]) + self.subsetsRecur(current + [sset[0]], sset[1:])
        return [current]
for name in py_solution.__dict__:
    print(name)

Sample Output:

__module__
sub_sets
subsetsRecur
__dict__
__weakref__
__doc__

Flowchart:

Flowchart: Create a class and display the namespace of the said class

For more Practice: Solve these Related Problems:

  • Write a Python program to define an empty class and print its __dict__ attribute to display its namespace.
  • Write a Python program to create a class with several attributes and methods, then display the keys of its __dict__.
  • Write a Python program to define a class with class-level variables and use dir() to show its namespace.
  • Write a Python program to create a class dynamically using type() and then print its namespace using __dict__.

Go to:


Previous: Write a Python program to import built-in array module and display the namespace of the said module.
Next: Write a Python program to create an instance of a specified class and display the namespace of the said instance.

Python Code Editor:

Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Based on 68 votes, average difficulty level of this exercise is Hard .

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.