Is it possible to check if a string is in a class nested in a class in Python?

42
November 13, 2019, at 4:00 PM

I have created a class in Python with classes nested in it:

class example:
     class example_in_class:
          exampleKey = "exampleItem"
          exampleKey2 = "exampleItem2"
          thisOne = "thisone"
          exampleKey3 = "exampleItem3"
          exampleKey4 = "exampleItem4"
     class example_in_class2:
          exampleKey = "exampleItem2"
          exampleKey2 = "exampleItem22"
          thisOne = "NOTthisone"
          exampleKey3 = "exampleItem32"
          exampleKey4 = "exampleItem42"

Now I want to see if I can find the item "thisone" without specifying the class.
Is it like example.include("thisone") or something?
I have searched this all over google but it isn't a human so I don't know how to word it so it understands...

Answer 1

I don't know why you would ever want to do such a thing, but here it is:

def find_thisone(cls):
    for attr in cls.__dict__.values():
        if type(attr) != type:
            continue
        if 'thisone' in attr.__dict__.values():
            return attr

This function will find you the nested class that has a string attribute with value thisone:

>>> find_thisone(example)
__main__.example.example_in_class

But for real, you're better off heeding the suggestions in the comments.

Edit: An explanation: .__dict__ allows you to iterate through an object's attributes just like a dictionary object. The dictionary has keys() and values(). The names of the nested classes will be in the keys(), and the actual classes will be in the values(). By checking whether the type() of each value is itself a type, i.e. a class, we can get at the nested classes without worrying about any other attributes of the outer class. Then we likewise access the attributes of the nested class using .__dict__.values() and check if the string 'thisone' is among them. If so, we've found the right nested class.

READ ALSO
Convert the multiple csv into mysql database

Convert the multiple csv into mysql database

I have an SQLite file of 18 million values, which I have changed into CSV files because It was not loading

19
Is there a way to detect if running code is being executed inside a context manager in python

Is there a way to detect if running code is being executed inside a context manager in python

As the title states, is there a way to do something like this:

34
Predicted standard deviation values of Gaussian Processes are only within [0, 1]

Predicted standard deviation values of Gaussian Processes are only within [0, 1]

I am using the Gaussian Processes of scikit-learn to estimate behavior of a black box likelihood function f()

46
The Python Generator abstract base class doesn't impliment the necessary __del__, is this intentional?

The Python Generator abstract base class doesn't impliment the necessary __del__, is this intentional?

Generator objects in Python are required to have a close method that exists to ensure that context managers are exited and try

42