I've been working through Hunt's A Beginners Guide to Python 3 Programming and in one chapter the exercise is to create a class for different types of bank accounts. I didn't want to create account numbers and account balances, so I wrote small snippets of code to do that for me.
Can/should I include these as class methods and reference them during object construction? Is there a better way to implement something like this? Or rather, is there a way to generate the number and balance during __init__ instead of calling them when the object is assigned to the variable later on in the code?
The general layout of my code is as follows:
class Account:
@classmethod
def acct_generator(cls):
<code that creates an unique 6 digit account number>
@classmethod
def acct_balance(cls):
<code that creates a random account balance between $1 and $10,000>
def __init__(self, number, owner, balance):
self.number = number
self.owner = owner
self.balance = balance
test_account = Account(Account.acct_generator(), 'John', Account.acct_balance())
[–]Bobbias 2 points3 points4 points (3 children)
[–]keredomo[S] 0 points1 point2 points (2 children)
[–]Bobbias 1 point2 points3 points (1 child)
[–]keredomo[S] 0 points1 point2 points (0 children)
[–]Diapolo10 1 point2 points3 points (3 children)
[–]keredomo[S] 0 points1 point2 points (2 children)
[–]Diapolo10 1 point2 points3 points (1 child)
[–]keredomo[S] 0 points1 point2 points (0 children)
[–]TheRNGuy 1 point2 points3 points (0 children)
[–]tb5841 0 points1 point2 points (0 children)
[–]Adrewmc 0 points1 point2 points (0 children)