mootools-depender icon indicating copy to clipboard operation
mootools-depender copied to clipboard

core.py

Open rogerhu opened this issue 14 years ago • 1 comments

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

rogerhu avatar Jul 08 '10 21:07 rogerhu

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".)

philz avatar Jul 08 '10 22:07 philz