Double underscore before a name isn't working in micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
akushva
Posts: 19
Joined: Fri May 03, 2019 10:08 am

Double underscore before a name isn't working in micropython

Post by akushva » Fri Jul 10, 2020 9:40 am

It's a sample code I executed in micropython.
class Student:
def __init__(self,name):
self.name=name
self._name1=name
self.__name2=name

p=Student('abc')
print(p.name)
print(p._name1)
print(p.__name2)

and the output I received
abc
abc
abc

p.__name generally generates an error in the python interpreter.

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

Re: Double underscore before a name isn't working in micropython

Post by stijn » Fri Jul 10, 2020 9:56 am

The class Student has no member __name, and the code you show runs fine without any error. Can you clarify what exactly the problem is?

*edit* ah, I assume you mean MicroPython does not support 'Private variables' i.e. https://docs.python.org/3/tutorial/clas ... -variables ?

akushva
Posts: 19
Joined: Fri May 03, 2019 10:08 am

Re: Double underscore before a name isn't working in micropython

Post by akushva » Fri Jul 10, 2020 10:07 am

stijn wrote:
Fri Jul 10, 2020 9:56 am
*edit* ah, I assume you mean MicroPython does not support 'Private variables' i.e. https://docs.python.org/3/tutorial/clas ... -variables ?

Yes, I meant that. I forgot to write "private variables" in my query :oops: .
So,upy doesn't support private variables?

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

Re: Double underscore before a name isn't working in micropython

Post by stijn » Fri Jul 10, 2020 10:56 am

No, at least not in the way CPython does. See tests/micropython/const.py for what it does have: private const variables.

You could open an issue on github to request it, but it's going to be low priority I guess: it's a more or less obscure feature which doesn't get much use.

Post Reply