76 interface.write(command)
77
78
79def control_cyborg_device(interface, command_function, data_function, interval=0.1): 80 """
81 Function to control a cyborg device using a command function and data function.
82
76 interface.write(command)
77
78
79def control_cyborg_device(interface, command_function, data_function, interval=0.1): 80 """
81 Function to control a cyborg device using a command function and data function.
82
76 interface.write(command)
77
78
79def control_cyborg_device(interface, command_function, data_function, interval=0.1): 80 """
81 Function to control a cyborg device using a command function and data function.
82
61 return data
62
63
64def write_actuator_command(interface, command): 65 """
66 Function to write a command to a cyborg actuator interface.
67
42 return interface
43
44
45def read_sensor_data(interface): 46 """
47 Function to read data from a cyborg sensor interface.
48
The local variable name hides the variable defined in the outer scope, making it inaccessible and might confuse.
filename = 'myfile.txt'
def read_file(filename): # This shadows the global `filename`
with open(filename) as file:
return file.readlines()
FILENAME = 'myfile.txt' # renamed global to UPPER_CASE as convention
def read_file(filename):
with open(filename) as file:
return file.readlines()
Another usual suspect of this is when you use the same parameter name inside a function as the global variable you are using. For example:
def run_app(app):
# This `app` shadows the global app...
app.run()
if __name__ == '__main__':
app = MyApp() # This is a global variable!
run_app(app)
To avoid this re-defining of a global, consider not defining app
as a global, but inside a main()
function instead:
def run_app(app):
# There is no longer a global `app` variable.
app.run()
def main():
app = MyApp()
run_app(app)
if __name__ == '__main__':
main()