Skip to content
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

core.py #4

Open
rogerhu opened this issue Jul 8, 2010 · 1 comment
Open

core.py #4

rogerhu opened this issue Jul 8, 2010 · 1 comment

Comments

@rogerhu
Copy link

rogerhu commented Jul 8, 2010

I could be missing something, but why are there so many lines that try to deal with None for package_ymls and script_jsons? Why not just set the package_ymls and scipt_jsons in the init_ declaration to [] as default values?

def init(self, package_ymls=[], script_jsons=[]):
"""
package_ymls is list of filenames; script_jsons
is list of (library_name, filename) pairs.
"""
if package_ymls is None: # remove
package_ymls = [] # remove
if script_jsons is None: # remove
scripts_json = [] #remove

self.packages = {}
self.unqualified_components = {}

self.script_json_packages = []

package_ymls = package_ymls or [] # remove
@philz
Copy link

philz commented Jul 8, 2010

It's very dangerous (from a programming practice and hair-pulling perspective) to use mutable objects as defaults in python. Those get evaluated once, when python runs through that function the first time. So, if you do something like package_ymls.append() in your method, it will apply to every future invocation of the function. I've been bitten by this enough times that even if I know I'm not modifying the argument, I never use mutable default arguments in python.

It's a common gotcha. http://www.deadlybloodyserious.com/2008/05/default-argument-blunders/ ; http://wiki.python.org/moin/PythonWarts ; and many more. (Search for "python default arguments" or "python warts".)

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

2 participants