Pascal int()

Michael Hudson mwh21 at cam.ac.uk
Fri Mar 17 09:45:57 EST 2000


Jeff Pinyan <jeffp at crusoe.net> writes:

> 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.

Tee hee.  Try this

http://www.deja.com/[ST_rn=ps]/getdoc.xp?AN=554921412&fmt=text

Warning: it's very silly.

BTW, you're code will have problems with local variables and being
executed across modules.

-- 
very few people approach me in real life and insist on proving they are
drooling idiots.                         -- Erik Naggum, comp.lang.lisp



More information about the Python-list mailing list