Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a def from a string
#3
FYI def has a name it's 'function' or 'method' if part of a class.

This is best done by creating a class named Room. Then when you want a room, you instantiate a new class.

Dumb Example (but gets the point across (I hope):
class Room:
    def __init__(self):
        self.current = None

    def dead(self):
        self.current = 'Dead'
    
    def alive(self):
        self.current = 'Alive'

class MyGame:
    def __init__(self):
        self.den = Room()
        self.den.alive()
        self.kitchen = Room()
        self.kitchen.alive()

    def display_rooms(self):
        print('\nden is {}'.format(self.den.current))
        print('kitchen is {}'.format(self.kitchen.current))
    
    def play(self):
        print('In the beginning')
        self.display_rooms()
        self.den.dead()
        print('A while later')
        self.display_rooms()
        self.den.alive()
        self.kitchen.dead()
        print('\nIn the end: ')
        self.display_rooms()

if  __name__ == '__main__':
    game = MyGame()
    game.play()
results:
Output:
In the beginning den is Alive kitchen is Alive A while later den is Dead kitchen is Alive In the end: den is Alive kitchen is Dead
Reply


Messages In This Thread
Call a def from a string - by DreamingInsanity - Jun-22-2018, 06:09 PM
RE: Call a def from a string - by buran - Jun-22-2018, 06:40 PM
RE: Call a def from a string - by Larz60+ - Jun-22-2018, 06:45 PM
RE: Call a def from a string - by ljmetzger - Jun-22-2018, 06:50 PM
RE: Call a def from a string - by volcano63 - Jun-22-2018, 08:03 PM
RE: Call a def from a string - by nilamo - Jun-22-2018, 08:04 PM
RE: Call a def from a string - by DreamingInsanity - Jun-23-2018, 08:21 AM
RE: Call a def from a string - by nilamo - Jun-25-2018, 06:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to call the values at the end of a text string? Dieselkaine 2 3,082 Jul-02-2018, 08:47 PM
Last Post: Dieselkaine

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020