Bearbeiten von „Python“
Zur Navigation springen
Zur Suche springen
Warnung: Du bist nicht angemeldet. Deine IP-Adresse wird bei Bearbeitungen öffentlich sichtbar. Melde dich an oder erstelle ein Benutzerkonto, damit Bearbeitungen deinem Benutzernamen zugeordnet werden.
Die Bearbeitung kann rückgängig gemacht werden. Bitte prüfe den Vergleich unten, um sicherzustellen, dass du dies tun möchtest, und veröffentliche dann unten deine Änderungen, um die Bearbeitung rückgängig zu machen.
Aktuelle Version | Dein Text | ||
Zeile 1: | Zeile 1: | ||
[[Kategorie:Sprache]] | [[Kategorie:Sprache]] | ||
= Exception = | = Exception = | ||
< | <pre> | ||
try: | try: | ||
raise Exception("not allowed") | raise Exception("not allowed") | ||
Zeile 18: | Zeile 14: | ||
finally: | finally: | ||
closeIt() | closeIt() | ||
</ | </pre> | ||
= Regular Expression = | = Regular Expression = | ||
< | <pre> | ||
rexpr = compile(r"([\da-z]+)") | |||
rexpr = | |||
match = rexpr.match(line) | match = rexpr.match(line) | ||
if match != None: | if match != None: | ||
number = (int) rexpr.group(1) | number = (int) rexpr.group(1) | ||
</pre> | |||
</ | |||
= Datentypen = | = Datentypen = | ||
* List: ['a', 1]; x[1] = 5; x.insert(0, 'firstItem'); x.remove('a'); ix=x.index('a'); del x[0]; | |||
* | * Tupel: t = ('a', 1); l = list(t) | ||
= | |||
* | |||
== | == Spezielle Methoden/Attribute == | ||
< | * Statische Methoden: | ||
<pre> | |||
class X: | |||
@staticmethod | |||
def doIt(param): | |||
pass | |||
X.doIt(True) | |||
* | </pre> | ||
* | * Feststellen, ob Attribut existiert: hasattr(instance, nameOfAttribute) | ||
* dynamischer Code: | |||
<pre> | |||
exec 'import ' + module | |||
</pre> | |||
== Dictionary== | == Dictionary== | ||
< | <pre> | ||
x = { 'key' : 'val', 'xyz': 3 } | x = { 'key' : 'val', 'xyz': 3 } | ||
x['key'] = value | x['key'] = value | ||
Zeile 87: | Zeile 51: | ||
contains = 'key' in x and 'key2' not in x | contains = 'key' in x and 'key2' not in x | ||
size = len(x) | size = len(x) | ||
for | for pair in x.iteritems(): | ||
key = pair[0] | |||
for key in x: | for key in x.iterkeys(): | ||
print key | print key | ||
</ | </pre> | ||
* x.itervalues() | * x.itervalues() | ||
* x.setdefault(key[, value]): setzt Wert nur, wenn noch nicht gesetzt | * x.setdefault(key[, value]): setzt Wert nur, wenn noch nicht gesetzt | ||
Zeile 99: | Zeile 63: | ||
== Mengen == | == Mengen == | ||
< | <pre> | ||
s = | s = set(['y', 3]) ; f = frozenset(['y', 3]) | ||
f = frozenset( | |||
for elem in s: | for elem in s: | ||
print elem | print elem | ||
Zeile 116: | Zeile 73: | ||
isPartOf = s <= f | isPartOf = s <= f | ||
diff = s - f | diff = s - f | ||
</ | </pre> | ||
== | == Dateien == | ||
<pre> | |||
with open(self._filename, "r") as fp: | |||
for line in fp: | |||
print line | |||
fp.close() | |||
</pre> | |||
< | |||
for | |||
</ | |||
== Typcheck == | == Typcheck == | ||
< | <pre> | ||
isStringOrSubclass = isinstance(aVariable, str) | isStringOrSubclass = isinstance(aVariable, str) | ||
isString = type(aVariable) is str | isString = type(aVariable) is str | ||
isList = type([1, 2]) is list | isList = type([1, 2]) is list | ||
isDict = type({ 0:"a", 1:"b" }) is dict | isDict = type({ 0:"a", 1:"b" }) is dict | ||
</ | </pre> | ||
== | == Formatierung == | ||
<pre> | |||
< | "{:3.7f} {:s} {:07d}".format(3.14, "hello", 100) | ||
"{0:d} / {0:x}".format(714) | |||
</pre> | |||
</ | |||
== Klasse als Sequenz == | == Klasse als Sequenz == | ||
Damit eine Klasse mit "x in classInstance" angesprochen werden kann, muss es einen Iterator geben. | Damit eine Klasse mit "x in classInstance" angesprochen werden kann, muss es einen Iterator geben. | ||
Im Beispiel wird dies in einer Klasse zusammengefasst:__iter__() liefert als Iterator sich selbst und __next__() implementiert diesen Iterator: | Im Beispiel wird dies in einer Klasse zusammengefasst:__iter__() liefert als Iterator sich selbst und __next__() implementiert diesen Iterator: | ||
< | <pre> | ||
class Example: | class Example: | ||
def __init__(): | def __init__(): | ||
Zeile 397: | Zeile 116: | ||
def next(): | def next(): | ||
return self.__next__() | return self.__next__() | ||
</ | </pre> | ||