Is this a legal / acceptable statement ?

  • Thread starter Philippe Martin
  • Start date
P

Philippe Martin

Hi,

This code works, but is it "appropriate" ?

l_init = False

if True == l_init and 1234 = l_value:
print 'l_value is initialized'

I know I can do this with a try but ...

Philippe
 
V

vbgunz

you don't have to say:

if True == l_init

it is suggested you simply say:

if l_init:

Remember the and operator requires expressions on both sides to be true
to continue. If you notice, your expression on the right side of the
'and' is an assignment and so this is forbidden (SyntaxError).
assignments only work on lines by themselves and no where else.

if you meant == rather than = remember this, l_value doesn't exist and
would pull up a NameError *but* because the first expression evaluates
as false the second expression is never evaluated.

refactor your code ASAP. good luck!
 
P

Philippe Martin

I'm sorry (typo):



l_init = False

if True == l_init and 1234 == l_value:
print 'l_value is initialized'


Note that 1234 == l_value does not get evaluated.

Philippe
 
L

Larry Bates

Philippe said:
Hi,

This code works, but is it "appropriate" ?

l_init = False

if True == l_init and 1234 = l_value:
print 'l_value is initialized'

I know I can do this with a try but ...

Philippe
1) You have a syntax error 1234 == l_value (note ==)
2) This doesn't test to see if l_value is initialized
because if it isn't you get:

Traceback (most recent call last):
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "junk.py", line 1, in ?
if True == l_init and 1234 == l_value:
NameError: name 'l_init' is not defined

3) It is unclear what l_init is used for in this context.
Setting it to True doesn't tell you anything about l_value.

Normally you do something like:

l_value=None
..
.. Intervening code
..
if l_value is None:
print "l_value uninitialized"

Or (something I never use):

if not locals().has_key('l_value'):
print "l_value uninitialized"


-Larry Bates
 
P

Philippe Martin

Larry said:
1) You have a syntax error 1234 == l_value (note ==)
2) This doesn't test to see if l_value is initialized
because if it isn't you get:

Traceback (most recent call last):
File
"C \Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File "junk.py", line 1, in ?
if True == l_init and 1234 == l_value:
NameError: name 'l_init' is not defined

3) It is unclear what l_init is used for in this context.
Setting it to True doesn't tell you anything about l_value.

Normally you do something like:

l_value=None
.
. Intervening code
.
if l_value is None:
print "l_value uninitialized"

Or (something I never use):

if not locals().has_key('l_value'):
print "l_value uninitialized"


-Larry Bates


l_init really is a boolean parameter and l_value a value that _might_ exist
in a shelve.

So I just want to have a parameter to a method so if the first value tested
is false (l_init) then the second (l_value) does not get tested ... because
it is the second in the statement and only seems to get evaluated if the
first one is true.

Philippe
 
B

bruno at modulix

Philippe said:
Hi,

This code works, but is it "appropriate" ?

appropriate for what ?-)
l_init = False
# corrected typo, cf other post in this thread
if True == l_init and 1234 == l_value:
print 'l_value is initialized'

Do this in production code, and have one of the first Python entry in
the Daily WTF !-)

What are you trying to do, exactly ? I've been scratching my head for at
least 5 minutes without finding any sensible reason to write such code.

I know I can do this with a try but ...

???


<ot>
Slightly ot, but since you ask for idioms:

1/ Since binding (so-called 'assignment') is a statement (not an
expression), you can as well write your equality test the 'normal' way:
.... pass

the "litteral value == variable" idiom comes from languages where
assignement being an expression, one could 'typo' '=' for '==', usually
leading to unwanted result !-)

2/ also, testing for equality against True or False is a bad idea, since
there are some things that will eval to false in a boolean context
without actually being equal to False:
for item in [[], (), {}, None, '']:
.... print item, " == False ?", item == False
.... if not item:
.... print "but still evals to False..."
....
[] == False ? False
but still evals to False...
() == False ? False
but still evals to False...
{} == False ? False
but still evals to False...
None == False ? False
but still evals to False...
== False ? False
but still evals to False...
so :.... pass

This still doesn't make sense to me, but it's at least a bit more
readable !-)
</ot>
 
B

bruno at modulix

Philippe Martin wrote:
(snip)
l_init really is a boolean parameter and l_value a value that _might_ exist
in a shelve.

So I just want to have a parameter to a method so if the first value tested
is false (l_init) then the second (l_value) does not get tested ... because
it is the second in the statement and only seems to get evaluated if the
first one is true.

s/seems to get/is/

But this is a really unpythonic way to do things IMHO. Either use a
try/except block (probably the most straightforward solution), or, as in
Larry's post, test for the existence of 'l_value' in locals().

My 2 cents...
 
F

Fredrik Lundh

Philippe said:
l_init really is a boolean parameter and l_value a value that _might_ exist
in a shelve.

So I just want to have a parameter to a method so if the first value tested
is false (l_init) then the second (l_value) does not get tested ... because
it is the second in the statement and only seems to get evaluated if the

how do you look things up in the shelve ?

</F>
 
M

Martin Blume

Hi,

This code works, but is it "appropriate" ?

l_init = False

if True == l_init and 1234 = l_value:
print 'l_value is initialized'

I know I can do this with a try but ...
I am a Python newbie, but I think working with
l_value = None
would be the most pythonic way.

C:\>c:\programme\python\python
Python 2.4 (#60, Nov 30 2004, 11:49:19)
[MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license"
for more information.
Of course in a function you can use:
def travel_time(from, to, speed=60):
pass

and if travel_time is called
travel_time(a,b,1000) the speed will be 1000
and if travel_time is called
travel_time(a,b) the speed will be 60

IMHO.
Martin
 
P

Philippe Martin

bruno said:
Philippe Martin wrote:
(snip)

s/seems to get/is/

But this is a really unpythonic way to do things IMHO. Either use a
try/except block (probably the most straightforward solution), or, as in
Larry's post, test for the existence of 'l_value' in locals().

My 2 cents...

Well, that was the question - I wanted to avoid that because I'm already in
a try/except and do not like to imbricate them too much.

Philippe
 
B

bruno at modulix

Philippe said:
bruno at modulix wrote:




Well, that was the question - I wanted to avoid that because I'm already in
a try/except and do not like to imbricate them too much.

Then reads Fredrik's answer and this:
'''
Help on module shelve:

(...)

DESCRIPTION
A "shelf" is a persistent, dictionary-like object.
(...)
"""

What about :

if shelf.has_key('l_value'):
...

?-)
 
P

Philippe Martin

bruno said:
Then reads Fredrik's answer and this:
'''
Help on module shelve:

(...)

DESCRIPTION
A "shelf" is a persistent, dictionary-like object.
(...)
"""

What about :

if shelf.has_key('l_value'):
...

?-)

Yes that would make sense.

Philippe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,207
Latest member
KazukoCape

Latest Threads

Top