From: mk on

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)


From: Steve Holden on
mk wrote:
>
> 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)
>
>
My own preference is for the latter, but I am sure you will find that
opinions are mixed on this. In recent versions of Python you might want
to take the possibility that cmd is Unicode into account by using

id isinstance(cmd, basestring):

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/

From: Jean-Michel Pichavant on
mk wrote:
>
> 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)
>
>
I would vote for the first one. But I could use the second as well, I
would'nt fight for it.

What is worrying me the most in your code sample is that self.cmd can
hold diferrent types (str, and something else). That is usually a bad
thing to do (putting None aside).
However, my remark could be totally irrelevant of course, that depends
on the context.

JM

From: mk on
Jean-Michel Pichavant wrote:
>
> What is worrying me the most in your code sample is that self.cmd can
> hold diferrent types (str, and something else). That is usually a bad
> thing to do (putting None aside).
> However, my remark could be totally irrelevant of course, that depends
> on the context.

That's a valid criticism - but I do not know how to handle this
otherwise really, because the program can be called with "cmd" to run,
or a script to run (or a directory to copy) and in those cases cmd is None.

I guess I could use

if cmd:
self.cmd = ...


But. Suppose that under some circumstances cmd is not string. What then?

I know that isinstance is typically not recommended, but I don't see
better solution here.


Regards,
mk


From: mk on
KDr2 wrote:
> cmd= isinstance(cmd,str) and c.replace('${ADDR}',ip) or cmd

Perlish, but I like that. :-)

Regards,
mk