Easy questions from a python beginner

wheres pythonmonks wherespythonmonks at gmail.com
Sun Jul 11 14:45:46 EDT 2010


Thanks for your answers -- it is much appreciated.

On #1:  I had very often used chained logic with both logging and
functional purposes in Perl, and wanted to duplicate this in Python.
"It reads like english"  Using the print_ print wrapper works for me.

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.

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.

On #3:  Sorry this is confusing, but I was browsing some struct array
code from numpy, in which one of the columns contained strings, but
the type information, supplied in numpy.array's dtype argument,
specified the type as a an "object" not a string.  Just wondering why
one would do that.

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

On #5: Nesting the function was actually what I was thinking of doing,
but alas, I cannot modify outer-scope variables within a function, and
of course, I don't want to use globals.

On #6: Always trying to improve my writing -- and I thought it was
cute that Guido tries to encourage this as well.


I am programmer who likes to scope off variables as much as possible
(I believe in minimal state).

The following is an example of what I am trying to protect against:
http://stackoverflow.com/questions/938429/scope-of-python-lambda-functions-and-their-parameters

Will try to avoid namespace mangling until next week.

Thanks again,

W



On Sun, Jul 11, 2010 at 2:17 PM, Duncan Booth
<duncan.booth at invalid.invalid> wrote:
> wheres pythonmonks <wherespythonmonks at gmail.com> wrote:
>
>> I'm an old Perl-hacker, and am trying to Dive in Python.  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?
>
> In Python 2.x print is a statement. If you really wanted you could do:
>
>   True and sys.write("It is true!\n")
>
> In Python 3 you can do this:
>
>   True and print("It is true!")
>
> though I can't think of any situations where this would be better that just
> writing:
>
>   if somecondition: print "whatever"
>
>>
>> 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??
>
> Why use a function?
>
>   x, y = y, x
>
>> (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)
>
> If you want to use perl's operators I suggest you use perl.
>
>>
>> 3.  Why might one want to store "strings" as "objects" in numpy
>> arrays?  (Maybe they wouldn't)?
>
> Why would one want to write incomprehensible questions?
>
>>
>> 4.  Is there a way for me to make some function-definitions explicitly
>> module-local?
>> (Actually related to Q3 below: Is there a way to create an anonymous
>> scope?)
>
> Not really.
>
>>
>> 5. Is there a way for me to introduce a indention-scoped variables in
>> python? See for example: http://evanjones.ca/python-pitfall-scope.html
>
> No. The page you reference effectively says 'my brain is used to the way
> Java works'. *My* brain is used to the way Python works. Who is to say
> which is better?
>
>>
>> 6.  Is there a Python Checker that enforces Strunk and White and is
>> bad English grammar anti-python?  (Only half joking)
>> http://www.python.org/dev/peps/pep-0008/
>>
> pylint will do quite a good job of picking over your code. Most people
> don't bother.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list