Alan Gauld via Tutor wrote:

> On 15/10/18 08:57, Peter Otten wrote:
> 
>> By the way, you do not need a map (dict) at all to implement a game like
>> this, you may return the next scene directly. A sketch:
>> 
>> class Bridge:
>>     def enter(self):
>>         ...
>>         action = ...
>>         if action == "jump off the bridge":
>>             return Death("You are eaten by the piranhas")
>>         else:
>>             ...
> 
> That was my initial thought when I saw this but thee is one
> caveat. The original design creates a single instance of a
> scene and returns that on each access. The suggestion above
> creates a new instance on every call. So if it is important
> to use the same instance each time then the map is a
> better solution.
> 
> (Although you could instead create a class variable holding
> the first instance of itself then use a class  constructor
> to either create the instance or access the class variable...)
> 

Or, to keep it really simple, return global class instances:

DEATH_BY_TORNADO = Death("The storm swept you off the bridge")

>> class Bridge:
>>     def enter(self):
>>         ...
>>         action = ...
           if action == "tornado":
               return DEATH_BY_TORNADO

One advantage of both variants is that tools like pylint are likely to catch 
spelling errors like the one that prompted the initial question.

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to