0

I've defined a main function in my class as well as a method to return the properties of the object using the method def __str__(self):. Being that I'm calling __repr__ inside of the class, is this the proper way of invoking this function? Also, is there a better alternative to having to use a long chain of if, elif, and else's?

Code:

def main(self):
    """Create usability of the account. Accessing the account
    should enable the user to check the account balance, make
    a deposit, and make a withdrawal."""
    main_menu = {1: 'Balance', 2: 'Deposit', 3: 'Withdraw', 4: 'Exit'}
    using_account = True

    while using_account:
        print '\nAvailable Choices:'
        print '\n'.join('%d.) %s' % (i, choice) for i, choice in
                        main_menu.iteritems())
        my_choice = int(raw_input('Enter a choice: '))
        if my_choice == 1:
            self.current_balance()
        elif my_choice == 2:
            self.deposit_funds()
        elif my_choice == 3:
            self.withdraw_funds()
        elif my_choice == 4:
            print self.__repr__()
            using_account = False
        else:
            print 'Invalid choice, try again!'
2
  • 1
    Use repr(self) instead. Commented Nov 30, 2013 at 22:20
  • @Blender this was killing me, thanks! Commented Nov 30, 2013 at 22:26

3 Answers 3

2

some_object.__repr__() is called when repr(some_object) is invoked (whether explicitly or implicitly). Calling it directly is fine, but most people would write repr(self) instead.

As to the chain of if statements, it's not messy enough yet to worry much about ;-) One alternative:

int2meth = {1: "current_balance", 2: "deposit_funds",
            3: "withdraw_funds", 4: "__repr__"}

You can store that dict anywhere (module level, class level, ...).

Then

methname = int2meth.get(my_choice)
if methname is None:
    print 'Invalid choice, try again!'
else:
    getattr(self, methname)()

Of course a similar thing could be done with a list instead of a dict. A dict gives another possibility: using strings for keys instead of meaningless little integers.

Sign up to request clarification or add additional context in comments.

2 Comments

Haha, thanks! I try to refactor as early as possible before things do get messy. Glad to know it wasn't as bas as I thought. I like this approach a lot.
It's a common approach, but has its own quirks. Most importantly here, the function assigned to integer 4 doesn't do using_account = False - you still need another way to worm around that. Anyway, if you're happy enough, please mark the question as finished by marking the answer as accepted.
0

Like all __magic__ methods __repr__ is supposed to be called via an operator or an operator-like builtin function - in this case either repr() or the ℅r format specifier.

Comments

0

... elif my_choice == 4: print self.__repr__() ...

What would happen if you did: print(self)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.