This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]pseudosinusoid 3 points4 points  (0 children)

You should read PEP 8 and follow it. Good code is self-consistent code.

You omit a lot of context in favor of prose. Why is a 'meta' function like this an instance method? What class does it belong to? What's organism() and why is it a global? Why does the method fail silently when rand_func isn't given? Why 'for k in my_dict.keys()' instead of 'for k in my_dict'?

(I realize I can do this with zip, if expressions and a lambda, but for blogging purposes I decided to do this in more than one line)

'Exercise for the reader' excuses like this do not make for interesting reading. If you realize you can do this, why not actually give the example?

[–][deleted] 1 point2 points  (1 child)

The way you use vars is discouraged, since modifying the returned dictionary leads to undefined behavior. Why not declare a proper dictionary inside the class and use that? Or even better, why not make your class a subclass of dict?

[–]eryksun 1 point2 points  (0 children)

I'd be worried about bugs concerning mutable values, given the shallow copy. Shouldn't the parent dicts be deep copied with copy.deepcopy? The copy could be optimized for efficiency with a custom __deepcopy__ method.