Viewing all class variables created within class instance

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

Viewing all class variables created within class instance

Post by kbrenner » Sun May 08, 2022 10:24 pm

After I create a class instance from test_script.py, I would like to view these variables. I know that in Pycharm, when running a Python script, you have the ability to view these variables in a variable pane; however, using the Micropython plugin, I am unable to view these variables in a the same way. Does anyone have any other suggestions? This is needed for validation that a class function is working properly.

I've tried calling dir(); however, it appears to only list out the modules that are imported as well as the name of the class instance without listing any of the class variables. Another solution in Python is to use os.environ.items(); however, I don't believe os in Micropython has the environ method built in. The same thing goes for calling %who or %whos.

I understand that you can call the .__dict__.keys() or .__dict__.values() commands on a class object to print out all class variables and their respective values; however, this is a somewhat unorganized and user-unfriendly approach to viewing class variables. I am looking for something that can provide a slightly better visual experience, since I need to validate several methods that are constantly changing these class variables.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: Viewing all class variables created within class instance

Post by davef » Sun May 08, 2022 10:53 pm

Try:

Code: Select all

dir(machine.ADC)
as a test then maybe you can modify that for your purposes.

Just a guess.

kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

Re: Viewing all class variables created within class instance

Post by kbrenner » Sun May 08, 2022 11:01 pm

Thanks, @davef. That provides me with a list that looks like:

['__class__','__name__','__bases__','__dict__','CORE_TEMP','CORE_VBAT','CORE_VREF','VREF','read_u16']

The only one of those that is callable on a class instance is '__dict__', which effectively just prints out a large dictionary.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Viewing all class variables created within class instance

Post by stijn » Mon May 09, 2022 5:57 am

Call dir() on your instance, not on the class itself. Like

Code: Select all

a = machine.ADC()
dir(a)

kbrenner
Posts: 46
Joined: Mon Jan 20, 2020 8:05 pm

Re: Viewing all class variables created within class instance

Post by kbrenner » Mon May 09, 2022 1:42 pm

I tried what you mentioned, though I don't necessarily see how it differs from the previously mentioned suggestion. Here is my output:
Machine.ADC Terminal Output.png
Machine.ADC Terminal Output.png (21.93 KiB) Viewed 2560 times

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Viewing all class variables created within class instance

Post by stijn » Mon May 09, 2022 2:03 pm

Without the brackets after ADC, you are not creating an instance of the class i.e. not calling the constructor.

Your first attempt was correct, but you have to supply arguments (as the error says), sorry but I left that out in my sample because i don't know them by heart plus they might be different depending on the board. See e.g. http://docs.micropython.org/en/latest/l ... e.ADC.html

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Viewing all class variables created within class instance

Post by stijn » Mon May 09, 2022 2:08 pm

For completeness the difference is this:

Code: Select all

class Foo:
    x = 0

    def __init__(self, y):
        self.y = 0

print(dir(Foo))
print(dir(Foo(1)))
output:

Code: Select all

['__class__', '__init__', '__module__', '__name__', '__qualname__', '__bases__', '__dict__', 'x']
['__class__', '__init__', '__module__', '__qualname__', '__dict__', 'x', 'y']
That y isn't available just by looking at the class itself, only after creating an instance since y gets assigned in the constructor.

Post Reply