Created
May 8, 2026 13:03
-
-
Save horstjens/76a92aac794b21481321e329255dcbb1 to your computer and use it in GitHub Desktop.
class_and_scope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| score = 100 | |
| # ändern vom root level ist erlaubt | |
| score += 1 | |
| print(score) | |
| # lesen von "unterhalb" also von innerhalb einer Funktion ist möglich | |
| #def play(): | |
| # print("ich weiß den top-level score:" , score) | |
| #play() | |
| # die kleine play funktion möchte auch den score verändern, darf aber nicht | |
| #def play(): | |
| # print("der score ist noch:", score) | |
| # print("ich will ihn verändern") | |
| # score += 1 | |
| # print("neuer score ist:", score) | |
| # # geht aber nicht, weil: | |
| # #UnboundLocalError: cannot access local variable 'score' where it is not associated with a value | |
| # die kleine play funktion darf nur einen Wert returnen | |
| # und im top-level wird dieser return-value der variable zugewiesen | |
| def play(): | |
| return score + 1 # anstatt score += 1 | |
| print("davor:", score) # 101 | |
| score = play() | |
| print("danach:",score) # 102 | |
| # andere möglichkeit (BITTE NICHT !!!! ) | |
| def play2(): | |
| global score # dieses keyword bitte NICHT verwenden (außer du willst unbedingt) | |
| score += 1 | |
| play2() | |
| print("score ist jetzt:", score) # 103 | |
| # wie kann ich machen, daß eine funktion einen top-level | |
| # wert ändern kann OHNE dass ich das hässliche global keyword | |
| # verwenden muss ? | |
| # 1. Variante: keine einfache variable verwenden, sondern eine | |
| # Datenstruktur wie zB eine Liste oder ein Dictionary | |
| # warum? eine Funktion darf zwar nicht eine top-level | |
| # variable überschreiben, aber sie darf ihre eingauten | |
| # funktionen benutzen, z.B an eine Liste mit append einen | |
| # Wert dranhängen | |
| scores = [100] # eine Liste anstatt einer einfachen int-variable | |
| def play3(): | |
| scores.append(1) # nicht überschrieben, aber benutzen | |
| print("vor play3:", scores) | |
| play3() | |
| print("nach play3:", scores) | |
| print(sum(scores)) | |
| # 2. Variante eine einfache int-variable machen, aber nicht | |
| # im top-level definieren sondern als | |
| # class-Variable! Die ist dann von überall her | |
| # (also auch innerhalb einer Funktion oder innerhalb | |
| # einer anderen Klasse) überschreibbar | |
| class Game: | |
| # class attribut: | |
| score = 0 # ansprechen mit Game.score | |
| def play4(): | |
| Game.score += 1 | |
| print("vor play4:", Game.score) # 0 | |
| play4() | |
| print("nach play4", Game.score) # 1 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # das self keyword | |
| # was ist eine Instanz einer Klasse | |
| # die Klasse Game dient nur als Sammelbecken für | |
| # globale Variablen, sogenannte class attributes: | |
| class Game: | |
| highscore = 0 | |
| score = 0 | |
| players = [] | |
| print(Game.__dict__) | |
| # output: | |
| """ | |
| {'__module__': '__main__', | |
| 'highscore': 0, 'score': 0, 'players': [], | |
| '__dict__': <attribute '__dict__' of 'Game' objects>, | |
| '__weakref__': <attribute '__weakref__' of 'Game' objects>, | |
| '__doc__': None} | |
| """ | |
| # Klassen können aber noch viel mehr, sie können nicht nur | |
| # class attribute verwalten, sondern sie können auch | |
| # instanzen habne, und jede instanz einer Klasse hat eigene | |
| # instanz-attribute und instanz-funktionen ( die nennt man Methoden ) | |
| class Enemy: | |
| # unique nummer für jeden enemy ( class attribute ) | |
| number= 0 | |
| # instanzen: | |
| def __init__(self): # das ist der "Construktor" | |
| # der wird automatisch ausgeführt | |
| # wenn eine neue Instanz erzeugt wird | |
| self.number = Enemy.number # ein instanz-attribut | |
| Enemy.number += 1 # manipuliert das class attribut | |
| self.x = 40 | |
| self.y = 77 | |
| self.aggro = 4 | |
| self.hitpoints = 100 | |
| self.status = "patrol" | |
| def attack(self): | |
| print("attacke!!!!") | |
| self.hitpoints -= 1 | |
| def meldung(self): | |
| print(self.__dict__) | |
| charly = Enemy() # erzeugt eine Instanz der enemy-klasse | |
| ivan = Enemy() # erzeugt noch eine Instanz | |
| charly.attack() # attacke! | |
| charly.meldung() # | |
| """ | |
| {'number': 0, | |
| 'x': 40, 'y': 77, 'aggro': 4, 'hitpoints': 99, | |
| 'status': 'patrol'} | |
| """ | |
| # variablen im speicher löschen | |
| def game2(): | |
| x = 555 | |
| # ..... | |
| return | |
| game2() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment