From: Carl Banks on
On Jul 11, 11:45 am, wheres pythonmonks <wherespythonmo...(a)gmail.com>
wrote:
> On #4:  So there are some hacks, but not something as easy as "import
> unimportable" or an @noexport decorator.  The underscore works, so
> does "del".

Careful. If you have a module that looks like this:


def foo():
bar()

def bar():
print "hello"

del bar # bar is an internal function


It won't work; foo will raise NameError on bar if you try that.
However, del is useful to clean up code you run at module import time,
for example:


squares = []
for i in xrange(101):
squares.append(i*i)
del i


Carl Banks
From: News123 on
Carl Banks wrote:
> On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo...(a)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]

From: Chris Rebert on
On Sun, Jul 11, 2010 at 2:08 PM, News123 <news1234(a)free.fr> wrote:
> Carl Banks wrote:
>> On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo...(a)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]:
^
SyntaxError: invalid syntax

Cheers,
Chris
From: News123 on
Chris Rebert wrote:
> On Sun, Jul 11, 2010 at 2:08 PM, News123 <news1234(a)free.fr> wrote:
>> Carl Banks wrote:
>>> On Jul 11, 10:48 am, wheres pythonmonks <wherespythonmo...(a)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)
From: MRAB on
Alf P. Steinbach /Usenet wrote:
> * Stephen Hansen, on 11.07.2010 21:00:
>> On 7/11/10 11:45 AM, wheres pythonmonks wrote:
>>> Follow-up:
>>> Is there a way to define compile-time constants in python and have the
>>> bytecode compiler optimize away expressions like:
>>>
>>> if is_my_extra_debugging_on: print ...
>>>
>>> when "is_my_extra_debugging" is set to false? I'd like to pay no
>>> run-time penalty for such code when extra_debugging is disabled.
>>
>> Any code wrapped in a __debug__ guard is utterly ommitted if you run
>> Python with the -O option. That, and asserts go away.
>>
>>> On #2: My point regarding the impossibility of writing the swap
>>> function for ints is to explicitly understand that this isn't
>>> possible, so as not to look for solutions along those lines when
>>> trying to write python code.
>>
>> Its impossible because Python's calling and namespace semantics simply
>> don't work like that. There's no references in the traditional sense,
>> because there's no variables-- boxes that you put values in. There's
>> just concrete objects. Objects are passed into the function and given
>> new names; that those objects have names in the enclosing scope is
>> something you don't know, can't access, and can't manipulate.. even the
>> objects don't know what names they happen to be called.
>>
>> Check out http://effbot.org/zone/call-by-object.htm
>
> Oh, I wouldn't give that advice. It's meaningless mumbo-jumbo. Python
> works like Java in this respect, that's all; neither Java nor Python
> support 'swap'.
>
> Of course there are variables, that's why the docs call them variables.
>
In Java a variable is declared and exists even before the first
assignment to it. In Python a 'variable' isn't declared and won't exist
until the first 'assignment' to it.