Easy questions from a python beginner
News123
news1234 at free.fr
Sun Jul 11 17:18:16 EDT 2010
Chris Rebert wrote:
> On Sun, Jul 11, 2010 at 2:08 PM, News123 <news1234 at free.fr> wrote:
>> Carl Banks wrote:
>>> On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo... at gmail.com>
>>> wrote:
>>>> I'm an old Perl-hacker, and am trying to Dive in Python.
>>> Welcome to the light.
>>>
>>>
>>>> I have some
>>>> easy issues (Python 2.6)
>>>> which probably can be answered in two seconds:
>>>>
>>>> 1. Why is it that I cannot use print in booleans?? e.g.:
>>>>
>>>>>>> True and print "It is true!"
>>>> I found a nice work-around using eval(compile(.....,"<string>","exec"))...
>>>> Seems ugly to this Perl Programmer -- certainly Python has something better?
>>> I'll repeat other people's sentiments: if you drop nothing else from
>>> your perl habits, drop this one.
>>>
>>>
>>>> 2. How can I write a function, "def swap(x,y):..." so that "x = 3; y
>>>> = 7; swap(x,y);" given x=7,y=3??
>>>> (I want to use Perl's Ref "\" operator, or C's &).
>>>> (And if I cannot do this [other than creating an Int class], is this
>>>> behavior limited to strings,
>>>> tuples, and numbers)
>>> Can't do it, but you can get reference-like behavior if you don't mind
>>> a level of indirection. For example:
>>>
>>> def swap(x,y):
>>> t = y[0]
>>> y[0] = x[0]
>>> x[0] = t
>>>
>>> a = [1]
>>> b = [2]
>>> swap(a,b)
>> or
>> def swap[x,y]:
>> x[0],y[0] = y[0],x[0]
>
>>>> def swap[x,y]:
> File "<stdin>", line 1
> def swap[x,y]:
apologies:
I meant
def swap(x,y):
x[0],y[0] = y[0],x[0]
a = [1]
b = [2]
swap(a,b)
More information about the Python-list
mailing list