Skip to content

library updates! #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ladyada opened this issue Oct 26, 2017 · 13 comments
Closed

library updates! #1

ladyada opened this issue Oct 26, 2017 · 13 comments

Comments

@ladyada
Copy link
Member

ladyada commented Oct 26, 2017

if const's have underlines, they wont be exposed outside the library, which i think makes sense for all the consts right? so instead of
TSL2561_DEFAULT_ADDRESS = const(0x39)
do
_TSL2561_DEFAULT_ADDRESS = const(0x39)


init should check the id and raise an RuntimeError if the chip is not found, the id() function can be inlined if you like - see https://github.com/adafruit/Adafruit_CircuitPython_BMP280/blob/master/adafruit_bmp280.py#L67


i know @tannewt mentioned changing lux to light but i actually just changed it back again (lux is a formal SI spec, light is just monotonic light measurements) so please make the property back to 'lux' (sorry!)

@caternuson
Copy link
Collaborator

My understanding is this is only a convention:
https://docs.python.org/3/tutorial/classes.html#private-variables
If we are going to do this, maybe it should be added to the style guide?


OK


Ha! It would appear we agree on this:
adafruit/circuitpython#314 (comment)

@dhalbert
Copy link
Contributor

In MicroPython/CircuitPython, adding the underscore actually prevents the name from going in the module dictionary. This is documented, but somewhat obscurely:

http://circuitpython.readthedocs.io/en/2.x/docs/reference/constrained.html?highlight=underscore

This RAM can be saved by prepending the name with an underscore as in _COLS: this symbol is not visible outside the module so will not occupy RAM.

http://circuitpython.readthedocs.io/en/2.x/docs/library/micropython.html?highlight=underscore

... if a constant begins with an underscore then it is hidden, it is not available as a global variable, and does not take up any memory during execution.

@caternuson
Copy link
Collaborator

So it's a micro/cp thing only? Just did this with CP 2.1:
foo.py:

SOME_CONST = 23
_SOME_CONST = 42

and then:

>>> import foo
>>> dir(foo)
['SOME_CONST', '_SOME_CONST', '__name__', '__file__']
>>> foo._SOME_CONST
42
>>> foo._SOME_CONST = 99
>>> foo._SOME_CONST
99

@ladyada
Copy link
Member Author

ladyada commented Oct 26, 2017

i believe it is only when we mpy the file

@caternuson
Copy link
Collaborator

just tried. same behavior.

@dhalbert
Copy link
Contributor

dhalbert commented Oct 26, 2017

Sorry, I was half-wrong. It's when you use const() and there's an underscore. It's true for both .py and .mpy (just tested):

from micropython import const
PUBLIC = const(2)
_PRIVATE = const(3)

try that

@dhalbert
Copy link
Contributor

dhalbert commented Oct 26, 2017

const() can only be an integer expression - nothing else. I apologize for the misleading information earlier.

@caternuson
Copy link
Collaborator

yep. that's it.
foo.py

from micropython import const
SOME_CONST = const(23)
_SOME_CONST = const(42)

def foo():
    print("{0} {1}".format(SOME_CONST, _SOME_CONST))

REPL:

>>> import foo
>>> dir(foo)
['const', 'SOME_CONST', 'foo', '__file__', '__name__']
>>> foo.foo()
23 42

@caternuson
Copy link
Collaborator

@dhalbert
Copy link
Contributor

Use an underscore for consistency and as notice that it's meant to be private. Maybe someday const will work on other than integers.

@caternuson
Copy link
Collaborator

Is that the answer, or a suggestion? Sounds like we should add this to the design guide.

@ladyada
Copy link
Member Author

ladyada commented Oct 26, 2017

i think both? :) we're updating the design guide ASAP, i just learned about this const stuff yesterday :D

@caternuson
Copy link
Collaborator

I think all three points in original post have been fixed, so closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants