which

M

mk

if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)
else:
self.cmd = cmd

or

self.cmd = cmd
if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)
 
P

Peter Otten

mk said:
if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)
else:
self.cmd = cmd

or

self.cmd = cmd
if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)

Neither.

self.cmd = cmd
self.ip = ip
....
subprocess.call(self.cmd, shell=True, env=dict(ADDR=self.ip))

Please spend a bit more energy on a descriptive subject and an unambiguous
exposition of your problem (in English rather than code) next time.

Peter
 
J

Jean-Michel Pichavant

G

Gerald Britton

[snip]
Last August [1], I offered this alternative:

 self.cmd = (cmd.replace(r'${ADDR}',ip)
             if isinstance(cmd, str) else
             cmd)

But it didn't get much love in this forum!

I'd probably go for that one as well though I might consider removing
the outer parentheses.
 
B

Bruno Desthuilliers

mk a écrit :
if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)
else:
self.cmd = cmd

What could "cmd" be except a string ? From other posts here, I guess
it's either a string or None ? If yes, then I'd go for this:

if cmd:
cmd = cmd.replace(r'${ADDR}',ip)

self.cmd = cmd
 
J

John Posner

[snip]
Last August [1], I offered this alternative:

self.cmd = (cmd.replace(r'${ADDR}',ip)
if isinstance(cmd, str) else
cmd)

But it didn't get much love in this forum!

I'd probably go for that one as well though I might consider removing
the outer parentheses.

Agreed ... except that you *need* the outer parentheses if the statement
occupies multiple source lines.

-John
 
J

John Posner

sure, but it will fit nicely on one line if you like

[snip]


Last August [1], I offered this alternative:

self.cmd = (cmd.replace(r'${ADDR}',ip)
if isinstance(cmd, str) else
cmd)

But it didn't get much love in this forum!

I'd probably go for that one as well though I might consider removing
the outer parentheses.
Agreed ... except that you *need* the outer parentheses if the statement
occupies multiple source lines.

-John
Did you mean to take this off-list? Also, I'm contractually obligated
to admonish you not to "top post".

At any rate, I proposed the 3-line format specifically because it
separates the data values from the if-then-else machinery, making it
easier (for me) to read. But there was considerable resistance to
spending so much vertical space in the source code.

-John
 
G

Gerald Britton

Did you mean to take this off-list?

Nope -- just hit the wrong key

 Also, I'm contractually obligated to
admonish you not to "top post".
Contract?


At any rate, I proposed the 3-line format specifically because it separates
the data values from the if-then-else machinery, making it easier (for me)
to read. But there was considerable resistance to spending so much vertical
space in the source code.

Weird! It's three lines and the original was four lines was it not>?
 
J

John Posner

Also, I'm contractually obligated to

Contract?

Joke. (I know it's hard to tell.)
Weird! It's three lines and the original was four lines was it not>?

Yes, but most people (including you, right?) seemed to think that
conditional expressions are best confined to a single line.

I proposed my 3-line alternative for expressions that *cannot*
reasonably be confined to a single line.

-John
 
S

Steven D'Aprano

if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)
else:
self.cmd = cmd

or

self.cmd = cmd
if isinstance(cmd, str):
self.cmd = cmd.replace(r'${ADDR}',ip)


Whichever one you like. The differences are insignificance, and
essentially boil down to personal preference.
 

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,176
Messages
2,570,950
Members
47,503
Latest member
supremedee

Latest Threads

Top