penjelmaan katak
penjelmaan katak

Reputation: 25

How can I call a method for multiple variables efficiently?

Here is a line of code from my program:

self.startcash, self.price, self.highroll, self.lowroll = self.prompt_user()

What you are seeing here is my attempt to get multiple variables from one method call. From my understanding of the PEP, it is unwise to put in so many variables in one line. Is there any way I can shorten these self calls/ break it down into multiple lines without calling the prompt_user method more than once?

NOTE: What Prompt_user does is that it gets input for the variables from users.

Upvotes: 0

Views: 56

Answers (2)

NotAName
NotAName

Reputation: 4347

If you really want to make it short and more readable, then you can just use tuple indexes, but that is likely to be a less readable option. Personally, I don't think there's much wrong with how you have it right now as long you don't have it like this everywhere. But anyway, you can convert your line:

self.startcash, self.price, self.highroll, self.lowroll = self.prompt_user()

to this:

output = self.prompt_user()

self.startcash = output[0]
self.price = output[1]
self.highroll = output[2]
self.lowroll = output[3]

Upvotes: 2

mazore
mazore

Reputation: 1024

The best way I know is to set them all to None (or some default value) in __init__

self.startcash = self.price = self.highroll = self.lowroll = None  # can make multiple lines

And then in your prompt_user function, you can set the variables using self, because you are using OOP

def prompt_user(self):
    self.startcast = input('startcash: ')
    self.price = input('price: ')
    self.highroll = input('highroll: ')
    self.lowroll = input('lowroll: ')

Upvotes: 0

Related Questions