Pascal int()

Jeff Pinyan jeffp at crusoe.net
Fri Mar 17 15:33:36 CET 2000


On Mar 17, Michal Bozon said:

>I want to have a function (of course in Python) equivalent to Pascal
>function int(). (It increments an integer stored in argument by 1).
>
>>>> int(i)
>>>> int(i, 2)

First, you'd better not call it int(), because that'd cause some sillyness
-- there's a builtin int().  For sake of argument, I'll call it inc().

  def inc(var,i=1):
    globals()[var] = globals()[var] + i

  foo = 20
  inc('foo')
  print foo  # 21
  inc('foo',10)
  print foo  # 31

Notice I passed the variable's NAME in a string.  That's the only way I've
figured out so far to muck with a global object like a string or number
inside a function.  I can't even assure that it's bug-free.

-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve             japhy at pobox.com
http://www.pobox.com/~japhy/                  http://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine            http://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc.    http://www.perlarchive.com/




More information about the Python-list mailing list