const() function
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: const() function
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.
Index to my micropython libraries.
Re: const() function
Yes, const works for class variables and function local variables (the latter could do with further optimisation).pythoncoder wrote: Can const() effectively be applied to class variables? It clarifies code to define constants relevant to a single class as class variables.
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: const() function
I noticed one thing that doesn't work with const():
Which should be perfectly valid Python syntax. Is this intended?
Code: Select all
NAME = ALIAS = const(42)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'const' is not defined
Re: const() function
Consider yes. Consider "const" as a special feature which works only for syntax of the form "NAME = const(<scalar_value>)".Is this intended?
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/
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/
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: const() function
So what if I do:
Is ALIAS also considered a constant then?
Code: Select all
NAME = const(42)
ALIAS = NAME
Re: const() function
No. Only object initialized with a const() function is consider a constant. For ALIAS to be a constant it would need to be:
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:ALIAS = const(NAME)
(That's also how it should be defined for CPython compatibility).const = lambda x: x
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/
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/
Re: const() function
Regarding "using const for class variable":
I define following class
which gives me:
Why?
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)
Code: Select all
>>> a =foo()
<class 'foo'>
24
<class 'type'>
24
badi
Re: const() function
I have have a feeling that this is related to this issue: https://github.com/micropython/micropython/issues/1675