NBA_01_Grundlagen¶
(c) 2026 Technische Hochschule Augsburg - Fakultät für Informatik - Prof.Dr.Nik Klever - Impressum
Einfaches Modul¶
In [1]:
%%writefile m1.py
print("im Modul m1.py: __file__: {}".format(__file__))
class c1():
def __init__(self):
print("in der Klasse c1 des Moduls m1.py")
def f1(self):
print("Name der Klasse: {}".format(self.__class__))
print("Name des Moduls: {}".format(self.__module__))
def f2(self):
print("Name der Funktion f2: {}".format(self.f2.__name__))
Writing m1.py
In [2]:
!python m1.py
im Modul m1.py: __file__: m1.py
In [3]:
import m1
im Modul m1.py: __file__: /home/student/SE2016FS02/Videos/Module/m1.py
In [4]:
type(m1)
Out[4]:
module
In [5]:
print(m1.__file__)
/home/student/SE2016FS02/Videos/Module/m1.py
In [6]:
i1 = m1.c1()
in der Klasse c1 des Moduls m1.py
In [7]:
type(i1)
Out[7]:
m1.c1
In [8]:
i1.f1()
Name der Klasse: <class 'm1.c1'> Name des Moduls: m1
In [9]:
i1.f2()
Name der Funktion f2: f2
Funktionsweise von Modulen¶
In [10]:
%%writefile testmodul.py
print("- Wir sind jetzt in testmodul.py")
print("- testmodul.py: __file__: {}".format(__file__))
print("- testmodul.py: __name__: {}".format(__name__))
if __name__ == "__main__":
print("- jetzt habe ich testmodul.py direkt aufgerufen")
else:
print("- Wir verlassen jetzt testmodul.py")
Writing testmodul.py
In [11]:
!python testmodul.py
- Wir sind jetzt in testmodul.py - testmodul.py: __file__: testmodul.py - testmodul.py: __name__: __main__ - jetzt habe ich testmodul.py direkt aufgerufen
In [12]:
%%writefile testvontestmodul.py
print("Wir sind jetzt in testvontestmodul.py")
import testmodul
print("Nach dem import von testmodul")
print("testvontestmodul.py: __file__: {}".format(__file__))
print("testvontestmodul.py: __name__: {}".format(__name__))
Writing testvontestmodul.py
In [13]:
!python testvontestmodul.py
Wir sind jetzt in testvontestmodul.py - Wir sind jetzt in testmodul.py - testmodul.py: __file__: /home/student/SE2016FS02/Videos/Module/testmodul.py - testmodul.py: __name__: testmodul - Wir verlassen jetzt testmodul.py Nach dem import von testmodul testvontestmodul.py: __file__: testvontestmodul.py testvontestmodul.py: __name__: __main__
Weiteres Beispiel für ein Modul: rechne.py¶
In [14]:
%%writefile rechne.py
def addiere(parameter1, parameter2):
return parameter1+parameter2
def multipliziere(parameter1,parameter2):
return parameter1*parameter2
if __name__ == "__main__":
print("test addiere: {}+{}={}".format(2,6,addiere(2,6)))
print("test multipliziere: {}*{}={}".format(4,5,multipliziere(4,5)))
Writing rechne.py
In [15]:
!python rechne.py
test addiere: 2+6=8 test multipliziere: 4*5=20
In [16]:
%%writefile verwende_rechne.py
import rechne
print("Addition: {}+{}={}".format(3,7,rechne.addiere(3,7)))
print("Multiplikation: {}*{}={}".format(6,8,rechne.multipliziere(6,8)))
Writing verwende_rechne.py
In [17]:
!python verwende_rechne.py
Addition: 3+7=10 Multiplikation: 6*8=48