Hi guys !
I'm new to micropython and python. I've bought an ESP8266 board and I'm trying to interface an SI4703 radio board in order to get a remotely controllable radio
My question is the following: when I run the following code in Python on my computer I have access to both "var_class" and "var_instance" whereas when I run it on micropython, I have only access to "var_class" !
[code]
class test:
var_class = 0
def __init__(self):
self.var_instance = 1
[/code]
Is this normal ? How to get access to the instance variable ?
instance variable in micropython
Re: instance variable in micropython
It would be useful if you could show that you did that worked in CPython that didn't work in MicroPython.
I tried this: and it works fine in both places. In order to access instance variables (i.e. var_iunstance) you need to instantiate an object, which is what the does.
I tried this:
Code: Select all
class test:
var_class = 0
def __init__(self):
self.var_instance = 1
inst = test()
print('inst.var_class = ', inst.var_class)
print('test.var_class = ', test.var_class)
print('inst.var_instance = ', inst.var_instance)
Code: Select all
inst.var_class = 0
test.var_class = 0
inst.var_instance = 1
Code: Select all
inst = test()
-
- Posts: 3
- Joined: Sun Apr 08, 2018 8:33 pm
Re: instance variable in micropython
Thank you Dave ! I don't understand what was wrong when I tried earlier but it works indeed for both perfectly
However, I do not have access to instance variables using inst.[TAB] for auto-completion in MicroPython. Do you have any advice for that ?
However, I do not have access to instance variables using inst.[TAB] for auto-completion in MicroPython. Do you have any advice for that ?
Re: instance variable in micropython
I built the latest MicroPython and flashed it to my pyboard and the completion seems to be working.
I created t.py containing: and typed the following: and it expanded var_i to var_instance
I created t.py containing:
Code: Select all
class test:
var_class = 0
def __init__(self):
self.var_instance = 1
Code: Select all
MicroPython v1.9.3-521-gd6cf5c6 on 2018-04-09; PYBv1.1 with STM32F405RG
Type "help()" for more information.
>>>
>>> import t
>>> inst = t.test()
>>> inst.var_i<TAB>
-
- Posts: 3
- Joined: Sun Apr 08, 2018 8:33 pm
Re: instance variable in micropython
Nice, thank you again Dave, I was using the stable firmware. I switched to the daily updated version of today and completion works like a charm for both class and instance variables. Now I'm ready to code !