const() function

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: const() function

Post by pythoncoder » Thu Dec 18, 2014 7:45 am

Thanks Damian for that clear explanation. Can const() effectively be applied to class variables? It clarifies code to define constants relevant to a single class as class variables.
Peter Hinch
Index to my micropython libraries.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: const() function

Post by Damien » Thu Dec 18, 2014 9:53 am

pythoncoder wrote: Can const() effectively be applied to class variables? It clarifies code to define constants relevant to a single class as class variables.
Yes, const works for class variables and function local variables (the latter could do with further optimisation).

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: const() function

Post by SpotlightKid » Thu Apr 09, 2015 9:29 pm

I noticed one thing that doesn't work with const():

Code: Select all

NAME = ALIAS = const(42)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'const' is not defined
Which should be perfectly valid Python syntax. Is this intended?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: const() function

Post by pfalcon » Sat Apr 11, 2015 11:04 am

Is this intended?
Consider yes. Consider "const" as a special feature which works only for syntax of the form "NAME = const(<scalar_value>)".
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: const() function

Post by SpotlightKid » Sat Apr 11, 2015 12:02 pm

So what if I do:

Code: Select all

NAME = const(42)
ALIAS = NAME
Is ALIAS also considered a constant then?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: const() function

Post by pfalcon » Sat Apr 11, 2015 12:54 pm

No. Only object initialized with a const() function is consider a constant. For ALIAS to be a constant it would need to be:
ALIAS = const(NAME)
And that may work or not as a constant underlyingly. Generally, const() is a hint to MicroPython compiler to consider constant propagation. It's semantics is equivalent to:
const = lambda x: x
(That's also how it should be defined for CPython compatibility).
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

User avatar
badi
Posts: 51
Joined: Mon Aug 10, 2015 2:18 pm
Location: Bern, Switzerland

Re: const() function

Post by badi » Tue Dec 08, 2015 4:10 pm

Regarding "using const for class variable":
I define following class

Code: Select all

class foo:
    stVar = const(42)
    stVar1=24
    def __init__(self):
        print(self.stVar)
        print(self.stVar1)
        print(foo.stVar)
        print(foo.stVar1)
which gives me:

Code: Select all

>>> a =foo()
<class 'foo'>
24
<class 'type'>
24
Why?
badi

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

Re: const() function

Post by dhylands » Tue Dec 08, 2015 6:35 pm

I have have a feeling that this is related to this issue: https://github.com/micropython/micropython/issues/1675

Post Reply