How to Get a List of Class Attributes in Python

In this tutorial, we have to get the list of attributes of the user-defined class. We have to define one class for it and derive the methods of the class in this class.

Python is an oops-based high-level programming language. In Object-oriented programming language, all things are objects. In objects, there are methods, properties, variables, and attributes.

To get a list of class attributes

In Python meaning of creating the new type of objects. Objects provide new functionality to the application and it bound together the data of application.Each class hase its attributes attached to it for maintaining its state.

Class is the keyword in Python.The synatx to  derived the class in Python is a very simple and minimum lines of code required for this.

class class_name():
  statement1
  statement2

  statement n

This is simplest syntax for derived the class.The synatx consist of the Class keyword and followed by the class name.

class simple():
  def __init__(self,para1):
    self.para1=para1
  def show(self):
    print(self.para1)
short=simple("class")
short.show()
print(dir(short))

In this above code we have derived one class simple and in this class we have derived one constructor and one method called show.The constructor is has one parameter para1 and we have made the one object of this class short and give the parameter as class and then we called the class .Then with the help of one predifined method dir() we printed the list of attributes in class.

Output

class
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'ak', 'show']

 

Leave a Comment

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

Scroll to Top