cyberbotics / webots

Webots Robot Simulator
https://cyberbotics.com
Apache License 2.0
3.21k stars 1.68k forks source link

Remove 'global' Variables from Python Code #1428

Open DavidMansolino opened 4 years ago

DavidMansolino commented 4 years ago

Describe the Bug Some of our python script are usign the global keyword, this is known as a bad practice (see for example: https://docs.quantifiedcode.com/python-anti-patterns/maintainability/using_the_global_statement.html) and can be avoided in 99% of the cases (personally I have never used it and never felt the need to use it):

ShuffleWire commented 2 years ago

I've just look that up, and there is 3 usage of the global variables : (for instance in resources/web/server/session_server.py)

To generalize, are there some unspoken rules about it in the Webot code. Do you thing it should be relevant to spend a bit of time to rearrange the code to make those global disappear ? I should be able to do so. The main question is also about useless global statement to read global variable : removing those global statement will hide the fact that there are global stuff running around. Is that fine ? TBF, it's common in python scripting... And if we don't mess with it (ie don't blindly read/write), it should be fine.

omichel commented 2 years ago

This is not an important issue and there is a risk to introduce bugs while modifying this code. So, I would prefer that you contribute something else.

ShuffleWire commented 2 years ago

That's fine, but just for the record, in R2022a, here is the list of python script using the keyword global (and the number of occurrences) scripts/converter/convert_geometries.py (1) projects/robots/mobsya/thymio/controllers/thymio2_aseba/aseba/maintainer/updatedoc/wikidot/structure.py (5) projects/robots/mobsya/thymio/controllers/thymio2_aseba/aseba/maintainer/translations/translation_tools.py (2) projects/default/resources/sumo/tools/traci/main.py (1) tests/test_suite.py (4) src/controller/matlab/mgenerate.py (2) resources/web/server/session_server.py (9) resources/web/server/simulation_server.py (23) resources/webots_ros/scripts/ros_controller.py (1) resources/webots_ros/scripts/ros_python.py (2)

stefaniapedrazzi commented 1 year ago

global variables are still used in webots_ros and webots_server repositories.