_winreg newbie question
Joe Green
the_3_project at yahoo.com
Tue Apr 29 08:29:21 EDT 2003
Duncan Booth <duncan at NOSPAMrcp.co.uk> wrote in message news:<Xns936C72D87CE5Dduncanrcpcouk at 127.0.0.1>...
> the_3_project at yahoo.com (Joe Green) wrote in
> news:a5b86892.0304290139.6870fb2b at posting.google.com:
>
> > if (key is "LOCAL_MACHINE"):
>
> What do you think this line does? It doesn't compare two strings to see if
> they are equal.
>
> Perhaps you meant:
>
> if key == 'LOCAL_MACHINE':
YES! That's what I'm trying to do :)
>
> The 'is' operator checks whether two expressions both result in exactly the
> same object. This is not something you want to do very often.
Very well, thanks for the hint.
> Usually objects can be equal without being the same object. (You can use
> 'is' to check whether an object 'is None' but that is because there is
> guaranteed only to be one None object in the system.)
>
Makes sense.
> Some other style points (feel free to disagree/ignore):
Oh no, this is very helpful stuff. However, I was trying to minimize
the code and put in a bare minimum to demonstrate the problem -- this
is not really how the real implementation looks.
>
> Never use 'from <module> import *' unless the module documentation insists
> you should (_tkinter does). It is likely to come back and bite you later.
>
Ah.. yes, normally I don't. In cases such as this, I do because I know
there will not be conflicts.
> Get rid of the unnecessary parentheses in your 'if' statements. Python is
> not C.
>
They are for my sanity. I do come from a C++ background :) I don't
usually use them unless there are logical and's and or's in the if
statement. Again, this was just for the demonstration of the problem.
> There are several ways to avoid that unsightly list of if...elif
> statements. Perhaps the easiest would be:
>
> def IterKeys(self, key, subkey=None):
> regkey = getattr(self, key, None)
> ...
>
> and then use regkey in place of 'self.reg[mykey]'.
>
Ah. Now that's something I can incorporate. Let's see how that works.
> Calling sys.exit when you cannot create the class seems a bit drastic. You
Yeah of course, drastic measures were needed to emphasise :)
> might prefer to throw a more specific exception instead, or even just
Right. That's what the actual implementation does.
> remove your try...except block and handle the exception at an outer level.
> As a general point an except block that catches every exception should make
> sure it does something useful (such as logging or displaying the error that
> actually occurred).
I do both whenever possible. Did I say I come from a Linux background?
We like them logs :)
Thanks a lot!
More information about the Python-list
mailing list