- Jul 20, 2018
- 724
- 1,185
I felt like doing a little bit of coding rather than working on my current projects. So I decided to be overly ambitious and start doing things waaaay outside of my python skill level
I ran into a problem trying to call info from a class so I can use it in a screen. After a couple of hours of googling and experimentation I understand I have to use the __getitem__ dundermethod.
I just wanted to know if what I'm doing is the best way to do it or if there is a better way. As you can see it will become a very large function with a class like this. The examples online all use really small classes as an example to keep it simple but in this case maybe a little to simple.
(I'm sure similar questions will arise when I start looking into __setitem__ )
I ran into a problem trying to call info from a class so I can use it in a screen. After a couple of hours of googling and experimentation I understand I have to use the __getitem__ dundermethod.
I just wanted to know if what I'm doing is the best way to do it or if there is a better way. As you can see it will become a very large function with a class like this. The examples online all use really small classes as an example to keep it simple but in this case maybe a little to simple.
Python:
class business:
def __init__(self, name, description, status, wealth, cost, LT, base, mod, store, stats, hourleave, houropen, hourclose, dayopen, workers, workercorrupt, workermax, upgrades):
self.name = name #string
self.description = description #tuple of strings
self.status = status #string
self.wealth = wealth #lib
self.cost = cost #integer
self.LT = LT #string
#etc...
def __getitem(self,key):
if key == 'name':
return self.name
elif key == 'description':
return self.description
elif key == 'status':
return self.status
#etc...
Last edited: