473,385 Members | 1,661 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Is this a legal / acceptable statement ?

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
May 5 '06 #1
12 1051
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!

May 5 '06 #2
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


vbgunz wrote:
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!


May 5 '06 #3
Philippe Martin wrote:
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
May 5 '06 #4


Larry Bates wrote:
Philippe Martin wrote:
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

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


May 5 '06 #5
Philippe Martin wrote:
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:
if l_init == True and l_value == 1234: .... 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 : if l_init and l_value == 1234:

.... pass

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

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 5 '06 #6
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...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 5 '06 #7
Philippe Martin wrote:
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>

May 5 '06 #8
"Philippe Martin" schrieb
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.
v = None
x = 2
x 2 v
v_default=3
y = x + (v or v_default)
y 5 v = 6
y = x + (v or v_default)
y 8 v = None
y = x + (v or v_default)
y 5


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

May 5 '06 #9
bruno at modulix wrote:
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...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"


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
May 5 '06 #10
Philippe Martin wrote:
bruno at modulix wrote:

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...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"

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'):
...

?-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
May 5 '06 #11
bruno at modulix wrote:
Philippe Martin wrote:
bruno at modulix wrote:

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...
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"

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'):
...

?-)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"


Yes that would make sense.

Philippe

May 5 '06 #12
Fredrik Lundh wrote:
Philippe Martin wrote:
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>

l_value = m_data['MY_KEY']

Philippe

May 5 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: J. Muenchbourg | last post by:
I'm getting an "Arguments are of the wrong type, out of acceptable range, in conflicts with each other " error , pointing to the sql statement in this block: Dim rstime Set rstime =...
7
by: Nice | last post by:
Hi. I read somewhere that in XML the tag <xml> is not legal. But in W3C specs I can't find this statement. Does anyone know about it ?
11
by: puzzlecracker | last post by:
is it possible to call nonconst member function on an unnamed temprary? ex: class String{ public: void doSomething(); }; String createString();
14
by: Ryan | last post by:
I want to do the following SQL statement in Access. However, it won't allow me to have the secondary part of my join statement and tells me that this is not supported. OK, so Access doesn't support...
5
by: Carlos Moreno | last post by:
I just noticed that from a C or C++ program using libpq or libpq++, I can send *one* command that contains several SQL statements separated by semicolon. Something like: PgDatabase db (" .......
3
by: Roy Hills | last post by:
I'm writing a local patch, which will be applied to several different source files. To prevent my local variables from clashing with those in the main source file, I want to create a new scope. ...
29
by: Ancient_Hacker | last post by:
A while back I had to recompile some old code, originally written by a really good programmer, but one prone to use every trick in the book, and then some. There was a statement, something like...
6
by: Dave Vandervies | last post by:
I'm writing an abstraction layer over an OS's ability to do non-blocking reads and writes on various things. The basic idea is that the caller will be able to let the I/O happen in the background,...
23
by: Boltar | last post by:
By accident I came across a bug like this in some Linux code I'd written today. All that had happened is I forgot the "else" yet the code still compiled and ran. A simplified example is below. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.