Python erkunden¶
Egal, ob ihr IDLE oder die Interaktive Shell nutzt, es gibt einige nützliche Funktionen, um Python zu erkunden.
>>> x = 4.2
type()¶
Mit type() könnt ihr euch den Objekttyp anzeigen lassen, z. B.:
>>> type(x)
<class 'float'>
help()¶
help() hat zwei verschiedene Modi. Wenn ihr help() eingebt, ruft
ihr das Hilfesystem auf, mit dem ihr Informationen zu Modulen, Schlüsselwörtern
und weiteren Themen erhalten könnt. Wenn ihr euch im Hilfesystem befindet, seht
ihr mit help> eine Eingabeaufforderung. Ihr könnt nun einen Modulnamen
eingeben, z.B. float, um die Python-Dokumentation zu diesem Typ zu durchsuchen.
help() ist Teil der pydoc-Bibliothek, die
Zugriff auf die in Python-Bibliotheken integrierte Dokumentation bietet. Da jede
Python-Installation mit einer vollständigen Dokumentation ausgeliefert wird,
habt ihr auch offline die gesamte Dokumentation zur Hand.
Alternativ könnt ihr help() auch gezielter anwenden, indem ihr einen
Typ- oder Variablennamen als Parameter übergebt, z. B.:
>>> x = 4.2
>>> help(x)
Help on float object:
class float(object)
| float(x=0, /)
|
| Convert a string or number to a floating point number, if possible.
|
| Methods defined here:
|
| __abs__(self, /)
| abs(self)
...
| is_integer(self, /)
| Return True if the float is an integer.
...
So erfahrt ihr z. B., dass x vom Typ float ist
und eine Funktion is_integer() hat, die ihr mit Punkt-Notation verwenden
könnt:
>>> x.is_integer()
False
id()¶
id() gibt die Identifikationsnummer eines Objekts an, z. B.:
>>> id(x)
4304262800
dir()¶
dir() ist eine weitere nützliche Funktion um herauszufinden, welche
Methoden und Daten lokal oder für ein bestimmtes Objekt verfügbar sind:
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'x']
So können wir uns z.B. mit dir(__builtins__) eine
Liste dessen anzeigen lassen, was in der Python-Standardbibliothek bereits
verfügbar ist:
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', 'BaseException', 'BaseExceptionGroup', 'BlockingIOError', 'BrokenPipeError', 'BufferError', 'BytesWarning', 'ChildProcessError', 'ConnectionAbortedError', 'ConnectionError', 'ConnectionRefusedError', 'ConnectionResetError', 'DeprecationWarning', 'EOFError', 'Ellipsis', 'EncodingWarning', 'EnvironmentError', 'Exception', 'ExceptionGroup', 'False', 'FileExistsError', 'FileNotFoundError', 'FloatingPointError', 'FutureWarning', 'GeneratorExit', 'IOError', 'ImportError', 'ImportWarning', 'IndentationError', 'IndexError', 'InterruptedError', 'IsADirectoryError', 'KeyError', 'KeyboardInterrupt', 'LookupError', 'MemoryError', 'ModuleNotFoundError', 'NameError', 'None', 'NotADirectoryError', 'NotImplemented', 'NotImplementedError', 'OSError', 'OverflowError', 'PendingDeprecationWarning', 'PermissionError', 'ProcessLookupError', 'PythonFinalizationError', 'RecursionError', 'ReferenceError', 'ResourceWarning', 'RuntimeError', 'RuntimeWarning', 'StopAsyncIteration', 'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError', 'SystemExit', 'TabError', 'TimeoutError', 'True', 'TypeError', 'UnboundLocalError', 'UnicodeDecodeError', 'UnicodeEncodeError', 'UnicodeError', 'UnicodeTranslateError', 'UnicodeWarning', 'UserWarning', 'ValueError', 'Warning', 'ZeroDivisionError', '_', '_IncompleteInputError', '__build_class__', '__debug__', '__doc__', '__import__', '__loader__', '__name__', '__package__', '__spec__', 'abs', 'aiter', 'all', 'anext', 'any', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes', 'callable', 'chr', 'classmethod', 'compile', 'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod', 'enumerate', 'eval', 'exec', 'exit', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'license', 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord', 'pow', 'print', 'property', 'quit', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice', 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip']