Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
closed by

Is there an approach to snatch a list of properties that exist on occasions of a class? 

class new_class():

    def __init__(self, number):

        self.multi = int(number) * 2

        self.str = str(number)

a = new_class(2)

print(', '.join(a.SOMETHING))

The ideal outcome is that "multi, str" will be yield. I need this to see the current ascribes from different pieces of a content.

closed

4 Answers

0 votes
by (25.7k points)
selected by
 
Best answer
Yes, you can retrieve a list of properties that exist on instances of a class using the dir() function and filtering out the built-in attributes. Here's an example:

class NewClass:

    def __init__(self, number):

        self.multi = int(number) * 2

        self.str = str(number)

a = NewClass(2)

properties = [attr for attr in dir(a) if not attr.startswith('__')]

property_list = ', '.join(properties)

print(property_list)

Output:

multi, str

In the above code, the dir(a) function returns a list of all attributes and methods of the instance a. By filtering out the attributes that start with __, which are built-in attributes, we obtain a list of properties specific to the instance.

We then use a list comprehension to iterate over the attributes, excluding those starting with __. Finally, the ', '.join() function is used to concatenate the properties into a single string separated by commas.

The result is the desired output, which is the list of properties: multi, str.
0 votes
by (26.4k points)

Check the below code:

>>> class new_class():

...   def __init__(self, number):

...     self.multi = int(number) * 2

...     self.str = str(number)

... 

>>> a = new_class(2)

>>> a.__dict__

{'multi': 4, 'str': '2'}

>>> a.__dict__.keys()

dict_keys(['multi', 'str'])

Are you pretty much interested to learn python in detail? Come and join the python training course to gain more knowledge.

0 votes
by (15.4k points)
class NewClass:

    def __init__(self, number):

        self.multi = int(number) * 2

        self.str = str(number)

a = NewClass(2)

properties = [attr for attr in dir(a) if not attr.startswith('__')]

property_list = ', '.join(properties)

print(property_list)

Explanation:

You can obtain a list of properties that exist on instances of a class by using the dir() function and filtering out the built-in attributes. In the provided code, an instance of the NewClass is created with the value 2.

To retrieve the list of properties, the dir(a) function is used to obtain a list of all attributes and methods of the instance a. By excluding attributes that start with __ (which are built-in attributes), a filtered list of properties specific to the instance is obtained.

A list comprehension is used to iterate over the attributes and generate a new list containing only the desired properties. Finally, the ', '.join() function is applied to concatenate the properties into a single string with commas.

The result is the desired output, which is the list of properties: multi, str.
0 votes
by (19k points)
class NewClass:

    def __init__(self, number):

        self.multi = int(number) * 2

        self.str = str(number)

a = NewClass(2)

properties = ', '.join(attr for attr in dir(a) if not attr.startswith('__'))

print(properties)

Explanation:

To retrieve a list of properties that exist on instances of a class, you can use the dir() function to obtain all attributes and methods of the instance. By filtering out the built-in attributes that start with __, a list of specific properties is obtained.

In the provided code, an instance of the NewClass is created with the value 2. The properties are obtained by using a list comprehension within the ', '.join() function, which concatenates the properties into a single string separated by commas.

The result is the desired output, which is the list of properties: multi, str.

Related questions

Browse Categories

...