instance variable in micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
thriller91
Posts: 3
Joined: Sun Apr 08, 2018 8:33 pm

instance variable in micropython

Post by thriller91 » Sun Apr 08, 2018 9:00 pm

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 ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: instance variable in micropython

Post by dhylands » Mon Apr 09, 2018 4:48 pm

It would be useful if you could show that you did that worked in CPython that didn't work in MicroPython.

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)
and it works fine in both places.

Code: Select all

inst.var_class =  0
test.var_class =  0
inst.var_instance =  1
In order to access instance variables (i.e. var_iunstance) you need to instantiate an object, which is what the

Code: Select all

inst = test()
does.

thriller91
Posts: 3
Joined: Sun Apr 08, 2018 8:33 pm

Re: instance variable in micropython

Post by thriller91 » Mon Apr 09, 2018 10:37 pm

Thank you Dave ! I don't understand what was wrong when I tried earlier but it works indeed for both perfectly :lol:

However, I do not have access to instance variables using inst.[TAB] for auto-completion in MicroPython. Do you have any advice for that ?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: instance variable in micropython

Post by dhylands » Mon Apr 09, 2018 10:59 pm

I built the latest MicroPython and flashed it to my pyboard and the completion seems to be working.

I created t.py containing:

Code: Select all

class test:
  var_class = 0

  def __init__(self):
    self.var_instance = 1
and typed the following:

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>
and it expanded var_i to var_instance

thriller91
Posts: 3
Joined: Sun Apr 08, 2018 8:33 pm

Re: instance variable in micropython

Post by thriller91 » Thu Apr 12, 2018 7:32 pm

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 :geek: !

Post Reply