tags:

views:

241

answers:

4

Sorry. I am not trying to start any flame. My scripting experience is from Perl, and I am pretty new in Python.

I just want to check whether I can have the same degree of flexibility as in Python.

In Python :

page = form.getvalue("page")
str = 'This is string : ' + str(int(page) + 1)

In Perl :

$str = 'This is string : ' . ($page + 1);

Is there any way I can avoid int / str conversion?

+7  A: 

No, since Python is strongly typed. If you keep page as an int you can do the following:

s = 'This is string : %d' % (page + 1,)
Ignacio Vazquez-Abrams
Then again, it will coerce for you; there's no problem using %s instead of %r above.
Nicholas Riley
@Nicholas: Sure, but if you use `%s` instead then you lose the capabilities that `%d` has when doing string formatting.
Ignacio Vazquez-Abrams
It's nothing to do with strong typing really, just that the operation for adding a `str` and an `int` together is not defined. This is a deliberate choice, should the result of `'1'+2` be '12', '3', or 3?
gnibbler
Please someone explain to me how python is a strongly type language? i have always thought otherwise
gath
@gath: You're confusing strongly/weakly typed with statically/dynamically typed. Python is strongly, dynamically typed.
Ignacio Vazquez-Abrams
You do not need a tuple: `(page+1)` is sufficient.
EOL
@EOL: Force of habit.
Ignacio Vazquez-Abrams
@gath, Hard to answer in the comments - maybe you should open a question for it
gnibbler
Shouldn't it be `s = 'This is string : %d' % (int(page) + 1,)`? 'page` appears to be a string and needs to be explicitly converted into an `int`.
MAK
@gath: Open a new question on that topic. Better yet, look at all of these: http://stackoverflow.com/search?q=[python]+strongly+typed
S.Lott
@MAK: See the second sentence.
Ignacio Vazquez-Abrams
A: 

No. Python doesn't have the same level of polymorphism as perl. You can print anything, and mix and match floats and ints quite easily, and lots of things (0, '', "", () and []) all end up False, but no, it's not perl in terms of polymorphism.

wisty
The issue is strongly typed versus weakly typed, not polymorphism.
Ignacio Vazquez-Abrams
You could imagine that Perl values are functions of zero arguments with polymorphic return types. (But they're not.)
jrockway
@jrockway: Scalar::Util::dualvar
ysth
+1  A: 

It looks like page is a str

page = form.getvalue("page")
S = 'This is string : %d'%(int(page)+1)

otherwise make page an int

page = int(form.getvalue("page"))
S = 'This is string : %d'%(page+1)

For the record (and to show that this is nothing to do with strong typing), you can also do crazy stuff like this:

>>> class strplus(int):
...  def __radd__(self, other):
...   return str(int(other).__add__(self))
... 
>>> page = form.getvalue("page")
>>> page + strplus(1)
'3'
gnibbler
+1  A: 

You could use:

mystr = "This string is: %s" % (int(page) + 1)

... the string conversion will be automatic when interpolating into the %s via the % (string formating operator).

You can't get around the need to convert from string to integer. Python will never conflate strings for other data types. In various contexts Python can return the string or "representation" of an object so there are some implicit data casts into string forms. (Under the hood these call .__str__() or .__repr__() object methods).

(While some folks don't like it I personally think the notion of overloading % for string interpolation is far more sensible than a function named sprintf() (if you have a language with operator overloading support anyway).

Jim Dennis