205 return self._value
206
207
208def _make_method_callback(method_name):209 def method(self, *args, **kwargs):
210 def callback():
211 return getattr(self.value, method_name)(*args, **kwargs)
352 return target_file
353
354 def human_password_for(
355 self, identifier, digits=2, key='generate', per_word=3, words=4,356 ):
357 return Fault(
358 'bw secrets human_password_for',
141 return key, cryptotext
142
143 def _generate_human_password(
144 self, identifier=None, digits=2, key='generate', per_word=3, words=4,145 ):
146 """
147 Like _generate_password(), but creates a password which can be
1014 }
1015
1016
1017def build_attr_property(attr, default):1018 def method(self):
1019 attr_source = None
1020 attr_value = None
1014 }
1015
1016
1017def build_attr_property(attr, default):1018 def method(self):
1019 attr_source = None
1020 attr_value = None
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()