Archives for posts tagged ‘python’

Programming Ruby: Chapter 3

20 chapters of Python should be a foundation solid enough to let me start learning yet another language without mixing the new knowledge up. Well, at least I am gonna give it a try. So here’s what I found out while reading Programming Ruby.

When overriding a method in a subclass, the keyword super suffices on [...]

Learning Python: Chapter 20

Package imports require you to put __init__.py files in each directory in the package chain.
The modifier as provides a means of neatly aliasing package paths in import statements with short names.

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

Still learning Python, but …

after sifting through some slides about The Cobra Programming Language, I get the impression that Cobra might be a language worth keeping in mind.

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 14

Docstrings are available at runtime via the __doc__ attribute.
The reference count of any object is available via sys.getrefcount(object).

Learning Python: Chapter 13

while and for loops may have an else block that is executed if the loop terminates naturally, that is, not by a break statement.
There is a no-op statement called pass.
Assignments to variables do not return the assigned value; they are statements, not expressions.
You can build your own iterables by implementing a next method that raises [...]