Still learning #Python. Whizzing through the lessons thinking "this is too easy." Then, of course, I sit down to write my first "real" program, an ASCII maze generator, and have to keep looking up things I already "learned." I also need to learn the type hints better.
gist.github.com/Ovid/6f67ebb40…
A simple ASCII maze program
A simple ASCII maze program. GitHub Gist: instantly share code, notes, and snippets.Gist
Curtis "Ovid" Poe (he/him)
Unknown parent • • •That prints out:
{'maze': <class '__main__.Maze'>, 'return': <class 'str'>}
So I guess I'm doing it right?
(And thanks for the kind words!)
Füsilier Breitlinger
in reply to Curtis "Ovid" Poe (he/him) • • •exec()
). Do you know if that's still true?Curtis "Ovid" Poe (he/him)
in reply to Füsilier Breitlinger • • •bmaxv
in reply to Füsilier Breitlinger • • •@barubary
"Get a local variable"? Not sure what you mean.
All variables are local and available in the function that they are defined in.
Global variables require the keyword but are bad practice.
Füsilier Breitlinger
in reply to bmaxv • • •@bmaxv @mdione @phiofx
By "local variable" I mean something that does not leak into or "pollute" the surrounding scope(s). Something like:
This is trivial in languages that let you declare local variables or provide local scopes, but in Python you cannot declare local variables and there are no local scopes, only entire functions.
You can try to fake a local scope by using a function (like the "IIFE" idiom in JavaScript), but that only moves the problem:
The local variable is safely contained, but functions must be named in Python (OK, there are anonymous functions, but their bodies are limited to simple expressions), so now it is the function name that leaks into the surrounding scope. You still have to worry about name clashes and choosing unique identifiers, but this time for functions.
The only workaround I know is string eval:
This ensures that there are no name clashes.
tmp
is safely contained in theexec()
and won't clash with any identifier in the surrounding scope.Marcos Dione
in reply to Füsilier Breitlinger • • •bmaxv
in reply to Füsilier Breitlinger • • •@barubary @mdione @phiofx the answer I would give is that it's incorrect to worry about these things *in Python*.
You are meant to put things into different functions or different classes or different files or different modules if you have to.
If this is a concern for the problem you are solving, python is the wrong tool for your problem.
Marcos Dione
in reply to Füsilier Breitlinger • • •Leandro Leites
in reply to Curtis "Ovid" Poe (he/him) • • •Curtis "Ovid" Poe (he/him)
in reply to Leandro Leites • • •@Lleites Poetry project? Never heard of that.
So far, the types are rather interesting. I have one type warning in create_maze() that I had to add a hint to ignore (can't figure out why it's wrong), but so far, it's pretty cool!
Leandro Leites
in reply to Curtis "Ovid" Poe (he/him) • • •Introduction | Documentation | Poetry - Python dependency management and packaging made easy
python-poetry.org