Reputation: 53853
Can I just call the get_random_items()
function directly on the random_items
attribute or is this considered bad practice in Python?
class Items(object):
def __init__(self, tools, toys, food):
self.tools = tools
self.toys = toys
self.yellow = food
self.random_items = self.get_random_items([self.tools, self.toys, self.food])
def get_random_items(self, item_list)
# do stuff to get random items..
return random_items
If this is bad practice, what would be a better way to provide the random_items
?
Upvotes: 0
Views: 505
Reputation: 1938
It works. But if you do that, the value of random_items
will be calculated only one time during __init__
. You will always get the old value, although the values of tools
, toys
or food
are currently changed.
You should use here property
class Items(object):
def __init__(self, tools, toys, food):
self.tools = tools
self.toys = toys
self.yellow = food
@property
def random_items(self):
return self.get_random_items([self.tools, self.toys, self.food])
def get_random_items(item_list)
# do stuff to get random items..
return some_random_items
And you can use random_items
like attribute in other functions.
Upvotes: 1