welcome: please sign in
location: Python

Python

print "Hallo Welt."
# Hallo Welt.

Etwas komplexer:

#/usr/bin/python

class Greeter(object):
    """Ein Begrüßungsobjekt"""
    def __init__(self, name):
        self.name = name

    def spam(self, count):
        """Eine Spam-Methode"""
        for i in range(0, count):
            print "%s: Hallo %s." % (i, self.name)

Greeter("Welt").spam(3)
# 0: Hallo Welt.
# 1: Hallo Welt.
# 2: Hallo Welt.

Und mit Keyword-Arguments:

#/usr/bin/python

def greet(name='Welt', count=1):
   for i in range(0, count):
       print "Hallo %s." % name

greet()
# Hallo Welt.

greet(count=3)
# Hallo Welt.
# Hallo Welt.
# Hallo Welt.

greet(name='Sepp')
# Hallo Sepp.

greet('Sepp', 2)
# Hallo Sepp.
# Hallo Sepp.

Und noch etwas seltsamer:

class Greeter(object):

    def __init__(self, name):
        self.name = name

    def spam(self, count):
        return dict([('wert%s' % i, 'Hallo - zum %sten mal - %s.' % (i, self.name)) \
                     for i in range(0, count) \
                     if i != 7])

dict = Greeter("Welt").spam(10)

for key, value in dict.iteritems():
  print """Der Schluessel "%s" enthaelt "%s".""" % (key, value)
# Der Schluessel "wert3" enthaelt "Hallo - zum 3ten mal - Welt.".
# Der Schluessel "wert2" enthaelt "Hallo - zum 2ten mal - Welt.".
# Der Schluessel "wert1" enthaelt "Hallo - zum 1ten mal - Welt.".
# Der Schluessel "wert0" enthaelt "Hallo - zum 0ten mal - Welt.".
# Der Schluessel "wert6" enthaelt "Hallo - zum 6ten mal - Welt.".
# Der Schluessel "wert5" enthaelt "Hallo - zum 5ten mal - Welt.".
# Der Schluessel "wert4" enthaelt "Hallo - zum 4ten mal - Welt.".
# Der Schluessel "wert9" enthaelt "Hallo - zum 9ten mal - Welt.".
# Der Schluessel "wert8" enthaelt "Hallo - zum 8ten mal - Welt.".

Python (last edited 2008-07-14 09:55:40 by localhost)