Archives for posts tagged ‘code’

Learning Python: Chapter 19

from-statements let you expose your code to the amout of namespace pollution you like:
from some_module import some_function (like use CGI qw(header); in Perl)
Symbol tables of modules are accessed through the dictionary named __dict__.

Learning Python: Chapter 18

sys.modules is a dictionary that contains all loaded modules’ names as keys.

Learning Python: Chapter 17

Anonymous functions are declared with lambda instead of def.
Lambda definitions are expressions and may therefore not contain if-statements and the like.
map(foo, list1, list2, list3) is like [foo(*tuple) for tuple in zip(list1, list2, list3)].
By using yield instead of return to provide a return value, you create a function that returns a generator object that returns one [...]

Learning Python: Chapter 16

 

Perl
Python

my $x = 10;
sub foo
{
$x = 20;
}

x = 10
def foo():
global x
x = 20

Declaring a variable in a function as global is only necessary for reassignments, not for mere read-access.
def foo(x, y, z): pass
foo(10, 20, 30)
does the same as
[...]

Learning Python: Chapter 15

A function definition may be put inside an if-block.
if DEBUG:
  def do_something():
    print ‘useful debug information’
    do_some_work()
else:
  def do_something():
    do_some_work()

Learning Python: Chapter 11

Because Python doesn’t guess default values, a sequence assignment like fst, snd = some_sequence works iff some_sequence has length 2.
There is no auto-increment and no auto-decrement.
Names beginning with single or double underscore have special meaning.
Names of built-ins can be reassigned.
Combined comparisons for range checks: x < y <= z.

Learning Python: Chapter 7

Strings are immutable sequences of characters.
Single- and Double-Quoted Strings Are the Same, they both don’t interpolate at all.
Adjacent string literals are concatenated automatically.
A backslash in a string literal that is not part of an escape sequence is kept in the resulting string.
Unicode string literals are prefixed by the character u; \u und \U can be [...]

Template Toolkit - Now With Added Python Goodness!

I had some idiosyncratic learning experiences lately. I read something about two things I want to learn about and suddenly, without explicitly searching for it, stumble upon something that deals with both of them. This happened again this morning. The two things that I was already learning were Python and the Template Toolkit.
As a Python [...]

Learning Python: Chapter 6

L1 = [1, 2, 3]
L2 = L1
L1 == L2 and L1 is L2 both yield True. 
L1 = [1, 2, 3]
L2 = [1, 2, 3]
L1 == L2 yields True (same values), but L1 is L2 yields False (different objects).
[...]

Learning Python: Chapter 5

After four chapters of introductory details I finally arrived at the in-depth explanation of Python that I actually looked forward to. Here’s all I found noteworthy from a Perl programmer’s point of view.

There are functions for explicit type conversion between individual numeric types: int(3.1415), float(3), long(4).

After saying
[...]