Python equivalent for C module

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Tue Oct 21 12:22:38 EDT 2008


Derek Martin a écrit :
> On Mon, Oct 20, 2008 at 07:29:16PM +0200, Bruno Desthuilliers wrote:
>> This should have been:
>>
>>     fprintf(STDERR, "DEBUG: %s", msg);
> 
> No, it shouldn't have.  If I turn on debugging, I  want the debug
> messages to go to stdout, so that they can be captured along with the
> output (of which there is almost none anyway) to clearly indicate when
> they happened.

Then redirect stderr to stdout.

>> STDOUT is for *normal* program outputs. Debug informations, warnings, 
>> and all verbosity should go to STDERR.
> 
> That's your opinion,

Nope, that's what stderr and stdout were designed for.

> and I disagree.  Besides which, if you're running
> a program in debug mode, you're DEBUGGING... "normal" does not apply.

so you'd find it ok for, say, a compression program, to intermix 
compressed data (it's normal output) with debugging infos ? Well, it's 
your program so you're of course free to write it as you want...

> You're being rather presumptuous... you don't even know how my program
> is being used.

Nope. But I do know the difference between sterr and stdout. Call me 
names for it if you want, but I'm not sure I understand how it will help.

>>> Then in the modules that wanted to use it, I did:
>>>
>> >from debug import DEBUG, dprint
>>> But I got some weird behavior.  The imported copy
>> It's not a copy.
> 
> Actually, I'm pretty sure it is; i.e. there are two copies of the
> name:

of the name, yes. Not of the object. We obviously weren't talking about 
the same thing.

> one in the namespace of the module, and one in the namespace of
> the file into which I imported it.
 >
>  At the time they are created, they
> both point to the same object.  Is that not the very definition of a
> copy?  The object itself may exist only in one place, but it has two
> names; one in each namespace. 

Indeed.

>>> of DEBUG is
>>> read-only;
>> It's not read-only.
> 
> The *object* very much is: it is immutable.  The name of that object
> is DEBUG, and thus DEBUG is read-only.  You can make DEBUG point to a
> different object by binding a different value to it, but if that value
> is of an immutable type, it will still be a read-only object.

Here again, not talking about the same thing. And I do know what's an 
immutable object, and how name binding works in Python, thanks.

> In the sentence I wrote, as well as in general, "DEBUG" actually
> refers to two different things: the object bound to the name, and the
> name itself.  It's up to the reader to infer which sense is correct
> given the thing being said about it.  It just so happens that the
> English sentence I wrote refers to both simultaneously.

And that I was refering to the name when you were talking about the 
object and vice-versa.

>> Just use a fully qualified name, so you dont make DEBUG local:
>>
>> import debug
>> print debug.DEBUG
>> debug.DEBUG = True
>> print debug.DEBUG
> 
> Right, several people have already pointed this out.

Indeed. That's the pythonic solution to your problem. Since I can infer 
from what you wrote above that you have a correct understanding of name 
bindings and scopes in Python, I can only wonder why you don't just use 
thos solution.

>  Which leads me
> to believe that the point of your reply was to berate me into
> following your conventions,

???

This is just insane. This is how name binding works in Python. It's not 
"my convention", nor anyone else convention, it's just how the language 
works.

> which I have no interest in doing, in part
> because they are counterproductive to my goals,  and in part because
> they are counter to the way I've been programming for 25 years.

Bad news : Python is not C.

> Fortunately, it's not your call how I write my code.

Write you code the way you like, I just don't give a damn.

>> Now note that ALL_UPPER names are - by convention - considered 
>> 'constants'. If this is supposed to be altered, don't write it ALL_UPPER.
> 
> YOUR convention, not mine.

Nope, Python's coding standard. Not mine.

>> Also and FWIW, Python has a logging module in it's stdlib. Please use it 
>> instead of any half-backed squared-wheel homegrown solution.
> 
> Note that the correct possessive form of "it" is "its" with no
> apostrophe.

Note that this is an international newsgroup, that I'm not a native 
english speaker, and that this remark, in the context, is - by usenet 
standards, not mine - to be considered as a deliberate insult.

> This was the only thing of value which you contributed,

Glad to be helpful somehow.

> though really, using that is way overkill for my needs.  If I've
> written bad code, by all means, please correct it.   If I've written
> code in a style that you happen not to like, please feel free to keep
> that to yourself.

If all you have to contribute here is flaming and insulting people that 
at least try to answer your questions, please feel free to post your 
contribution somewhere else.

>> My 2 cents
> 
> Must be Zimbabwe currency...
> 

Godwin point ahead. Good luck Mr. Martin.





More information about the Python-list mailing list