Re: Overriding a global

2011-12-15 Thread Joshua Landau
On 14 December 2011 17:06, Jean-Michel Pichavant jeanmic...@sequans.comwrote: Joshua Landau wrote: [snip] Using currentLogger is just padding, in my opinion. *Every *value is currentvalue. Not always. I try to keep names on the same object because that object is supposed to be named that

Re: Overriding a global

2011-12-14 Thread Jean-Michel Pichavant
Joshua Landau wrote: On 13 December 2011 13:30, Jean-Michel Pichavant jeanmic...@sequans.com mailto:jeanmic...@sequans.com wrote: writing x = 1 def spam(): x = 2 is in general a bad idea. That was my point. Why? I have a few (probably wrong) guesses. Because you

Re: Overriding a global

2011-12-14 Thread Chris Angelico
On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: The problem makes little sense when using names like x or func1. Besides namespace issues, naming 2 *different objects* with the same meaningful name is usually a bad idea and points the fact that your names

Re: Overriding a global

2011-12-14 Thread Jean-Michel Pichavant
Chris Angelico wrote: On Wed, Dec 14, 2011 at 9:14 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: The problem makes little sense when using names like x or func1. Besides namespace issues, naming 2 *different objects* with the same meaningful name is usually a bad idea and points

Re: Overriding a global

2011-12-14 Thread Chris Angelico
On Wed, Dec 14, 2011 at 11:05 PM, Jean-Michel Pichavant jeanmic...@sequans.com wrote: Chris Angelico wrote: So... it's a bad idea for me to use 'i' many times in my code, with the same name having different meanings in different places? In languages with infinitely-nesting scopes... Bad

Re: Overriding a global

2011-12-14 Thread Steven D'Aprano
On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: Bad ideas : i = 5 def spam(): for i,v in enumerate([1,2,3,4]): for i,v in enumerate(['a','b', 'c']): print i, v print i,v # bad surprise The bad surprise happens because you are using the same name twice

Re: Overriding a global

2011-12-14 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Wed, 14 Dec 2011 13:05:19 +0100, Jean-Michel Pichavant wrote: Bad ideas : i = 5 def spam(): for i,v in enumerate([1,2,3,4]): for i,v in enumerate(['a','b', 'c']): print i, v print i,v # bad surprise The bad surprise happens because you are

Re: Overriding a global

2011-12-14 Thread Joshua Landau
On 14 December 2011 10:14, Jean-Michel Pichavant jeanmic...@sequans.comwrote: Joshua Landau wrote: On 13 December 2011 13:30, Jean-Michel Pichavant jeanmic...@sequans.commailto: jeanmic...@sequans.com** wrote: writing x = 1 def spam(): x = 2 is in general a bad

Re: Overriding a global

2011-12-14 Thread Jean-Michel Pichavant
Joshua Landau wrote: [snip] Using currentLogger is just padding, in my opinion. *Every *value is currentvalue. Not always. I try to keep names on the same object because that object is supposed to be named that way. I can change one of the object attribute, but the object named that way keep

Re: Overriding a global

2011-12-13 Thread Joshua Landau
On 13/12/2011, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau joshua.landau...@gmail.com wrote: Rebinding logger locally in a function is really no different to a subclass rebinding a variable from its main class using that class' value. The only

Re: Overriding a global

2011-12-13 Thread Antoon Pardon
On 12/10/2011 09:47 PM, Roy Smith wrote: I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other

Re: Overriding a global

2011-12-13 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care about making names globally unique. I don't get your point,

Re: Overriding a global

2011-12-13 Thread Steven D'Aprano
On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: Steven D'Aprano wrote: On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care

Re: Overriding a global

2011-12-13 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Tue, 13 Dec 2011 10:54:51 +0100, Jean-Michel Pichavant wrote: Steven D'Aprano wrote: On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: Using the same name for 2 different objects is a bad idea in general. We have

Re: Overriding a global

2011-12-13 Thread Joshua Landau
On 13 December 2011 13:30, Jean-Michel Pichavant jeanmic...@sequans.comwrote: writing x = 1 def spam(): x = 2 is in general a bad idea. That was my point. Why? I have a few (probably wrong) guesses. Because you expect it to be the same every time you use it? Well, then this should be

Re: Overriding a global

2011-12-13 Thread Ian Kelly
On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau joshua.landau...@gmail.com wrote: No, there is another difference, the reason for rebinding the name. In a subclass, you would rebind a class attribute because that particular attribute, which you need to change, is used and expected by external

Re: Overriding a global

2011-12-13 Thread Joshua Landau
On 13 December 2011 19:34, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau joshua.landau...@gmail.com wrote: No, there is another difference, the reason for rebinding the name. In a subclass, you would rebind a class attribute because that particular

Re: Overriding a global

2011-12-13 Thread Ian Kelly
On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau joshua.landau...@gmail.com wrote: On 13 December 2011 19:34, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau joshua.landau...@gmail.com wrote: No, there is another difference, the reason for rebinding the

Re: Overriding a global

2011-12-13 Thread Joshua Landau
On 13 December 2011 19:54, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Dec 13, 2011 at 12:43 PM, Joshua Landau joshua.landau...@gmail.com wrote: On 13 December 2011 19:34, Ian Kelly ian.g.ke...@gmail.com wrote: On Tue, Dec 13, 2011 at 1:34 AM, Joshua Landau joshua.landau...@gmail.com

Re: Overriding a global

2011-12-12 Thread Jean-Michel Pichavant
Roy Smith wrote: MRAB pyt...@mrabarnett.plus.com wrote: or use 'globals': def function(self): logger = globals()['logger'].getChild('function') logger.debug('stuff') logger.debug('other stuff') Ah-ha! That's precisely what I was looking for. Much

Re: Overriding a global

2011-12-12 Thread Steven D'Aprano
On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care about making names globally unique. -- Steven -- http://mail.python.org/mailman/listinfo/python-list

Re: Overriding a global

2011-12-12 Thread Dave Angel
On 12/12/2011 04:28 PM, Steven D'Aprano wrote: On Mon, 12 Dec 2011 12:13:33 +0100, Jean-Michel Pichavant wrote: Using the same name for 2 different objects is a bad idea in general. We have namespaces precisely so you don't need to care about making names globally unique. True, but in this

Re: Overriding a global

2011-12-12 Thread Joshua Landau
Wouldn't this be nicer, though?: def getChildLogger(id): return logger.getChild(id) def someFunc(): logger = getChildLogger(someFunc) -- UNTESTED -- No messing around with globals this way, and it's more extendable. And 'globals()[logger].getChild(someFunc)' reads like a brick. --

Re: Overriding a global

2011-12-12 Thread Ben Finney
Dave Angel d...@davea.name writes: True, but in this code, the function is trying to both use the global value, but also a local that deliberately has the same name, but a different meaning and value. The CPython compiler doesn't make this easy, and I think the globals() technique is

Re: Overriding a global

2011-12-12 Thread Joshua Landau
If a function knows of the presence of a global, it's not asking too much for it to not re-use the same name in local scope. Yes. It's just a function wanting to act as-if it were in a different environment than its default. By that same reasoning you could state that If a function knows

Re: Overriding a global

2011-12-12 Thread Dave Angel
On 12/12/2011 06:48 PM, Joshua Landau wrote: If a function knows of the presence of a global, it's not asking too much for it to not re-use the same name in local scope. Yes. It's just a function wanting to act as-if it were in a different environment than its default. By that same reasoning

Re: Overriding a global

2011-12-12 Thread Steven D'Aprano
On Tue, 13 Dec 2011 09:27:09 +1100, Ben Finney wrote: Dave Angel d...@davea.name writes: True, but in this code, the function is trying to both use the global value, but also a local that deliberately has the same name, but a different meaning and value. The CPython compiler doesn't make

Re: Overriding a global

2011-12-12 Thread Ian Kelly
On Mon, Dec 12, 2011 at 4:48 PM, Joshua Landau joshua.landau...@gmail.com wrote: Rebinding logger locally in a function is really no different to a subclass rebinding a variable from its main class using that class' value. The only difference is that, in that case, you have an alternate

Re: Overriding a global

2011-12-11 Thread Peter Otten
Roy Smith wrote: I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other stuff') and so

Overriding a global

2011-12-10 Thread Roy Smith
I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other stuff') and so on. This works, but every

Re: Overriding a global

2011-12-10 Thread MRAB
On 10/12/2011 20:47, Roy Smith wrote: I've got a code pattern I use a lot. In each module, I create a logger for the entire module and log to it all over: logger = logging.getLogger('my.module.name') class Foo: def function(self): logger.debug('stuff') logger.debug('other

Re: Overriding a global

2011-12-10 Thread Roy Smith
MRAB pyt...@mrabarnett.plus.com wrote: or use 'globals': def function(self): logger = globals()['logger'].getChild('function') logger.debug('stuff') logger.debug('other stuff') Ah-ha! That's precisely what I was looking for. Much appreciated. --

Re: Overriding a global

2011-12-10 Thread Terry Reedy
On 12/10/2011 3:47 PM, Roy Smith wrote: What I really want to do is: def function(self): Add a global statement to rebind a global name: global logger logger = logger.getChild('function') logger.debug('stuff') logger.debug('other stuff') which lets me not

Re: Overriding a global

2011-12-10 Thread Terry Reedy
On 12/10/2011 7:14 PM, Terry Reedy wrote: On 12/10/2011 3:47 PM, Roy Smith wrote: What I really want to do is: def function(self): Add a global statement to rebind a global name: global logger But I see that that is not what you want to do, which is to override the global name just