On 05/18/11 06:24 AM, William Schumann wrote:
Shawn,
Please find some questions at the bottom.
On 05/17/11 09:15 PM, Shawn Walker wrote:
On 05/17/11 11:16 AM, Keith Mitchell wrote:
On 05/17/11 11:12 AM, William Schumann wrote:
Keith,
I have one preliminary question:

On 05/16/11 07:29 PM, Keith Mitchell wrote:
...
ns_info.py:
...
225: This dict should be statically defined.

265-276: Statically define this somewhere.
What you mean by 'statically', and why?

I guess "global" would be more precise. Or rather, at the module level.

As written, every time the function is called, the dictionary is
reconstructed; and since the objects seemed to be only read-from, never
written-to, it seemed like they should be defined at the module level,
instead of as locals.

That's not true. Lists, dictionaries, tuples, etc. that can be
completely interpreted at compile time will be the same object every
time:

#!/usr/bin/python2.6
def test():
propdict = { 'key': 'value' }
print "propdict id: %s" % id(propdict)
for i in range(3):
test()

$ python /tmp/test.py
propdict id: 135258836
propdict id: 135258836
propdict id: 135258836

With that said, you're right that a dictionary like this may be more
appropriately defined as a "constant" at the module level.
You're the third reviewer who has asserted this without explanation
using terms that don't exist in Python (e.g., static, constant, feel,
bikeshed ;) ).
Please explain why it is more appropriate to define it at a module
level. It is executed only once, read-only, only locally. Why should the
maintainer of the code have to look elsewhere for the assignment of the
dict (where it requires extra commenting because it appears completely
out of context)?

Because defining the dictionary at the module level means that Python will always use the same object and reference for it. When you define a data structure inside a function, which object/reference Python assigns is based on usage:

#!/usr/bin/python2.6

MODULE_DICT = { "key": "value" }

def func_dict():
        return { "key": "value" }

a = None
for i in range(3):
        a = MODULE_DICT
        print "MODULE DICT: %s" % id(a)

b = None
for i in range(3):
        b = func_dict()
        print "func_dict: %s" % id(b)

MODULE DICT: 135267028
MODULE DICT: 135267028
MODULE DICT: 135267028
func_dict: 135268252
func_dict: 135268116
func_dict: 135268252

Python does actually have constants:
  http://docs.python.org/library/constants.html

Although admittedly, here, we're talking about constants by definition and convention as opposed to through language-specific syntax. I'd also point out that this is an incredibly nitty concern at best.

In your particular case (which seems to have been lost in the noise), 'propdict' isn't returned from the function in question, so the only justifications I could see someone using to ask that you define it at the module level would be:

  * your team convention is that read-only data structures are defined
    at the module level for easy reference

  * 'propdict' is used in more than this one place

I personally see no specific reason to define 'propdict' at the module level.

-Shawn
_______________________________________________
caiman-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/caiman-discuss

Reply via email to