Anoop K George
Anoop K George

Reputation: 1755

Python,unable to call initial arguments outside the class

The below python class have empty dictionary as initial arguments, after calling createAccount() outside class it successfully add data to dictionary but I can't access dictionary outside class.

What changes shall I make in the below code to access the newly created account details ?

*Please note that my error occurs in the last line of the code *

class SavingsAccount():
    def __init__(self):
        self.savingsAccounts = {}
    def createAccount(self, name, initialDeposit):
        print()
        self.accountNumber = int(12345)
        self.savingsAccounts[self.accountNumber] = [name, initialDeposit]
        print("Account creation has been successful. Your account number is ", self.accountNumber)


SavingsAccount().createAccount(name = 'a',initialDeposit=4)

print(SavingsAccount().savingsAccounts[12345]) # getting error here

Upvotes: 0

Views: 58

Answers (4)

Taohidul Islam
Taohidul Islam

Reputation: 5414

You should initialize your object using __init__,

class SavingsAccount:
    def __init__(self, name, initial_deposit):
        self.accountNumber = 12345
        self.savingsAccounts = {self.accountNumber : [name, initial_deposit] }
        print("Account creation has been successful. Your account number is ", self.accountNumber)


saving_account = SavingsAccount(name='a', initial_deposit=4)

print(saving_account.savingsAccounts)

Also, most of the Pythonistas prefer snake_casing while naming variables.

Upvotes: 4

chepner
chepner

Reputation: 531868

You are creating a new instance of SavingsAccount with every call. After you call to createAccount completes, that instance is garbage-collected, as there are no references to it stored anywhere.

s = SavingsAccount()

s.createAccount(name='a', initialDeposit=4)
print(s.savingsAccounts[12345])

(See Taohidul Islam's answer for how you should be defining the class, though.)

Upvotes: 2

Nathan Mathews
Nathan Mathews

Reputation: 153

Must first initialize an instance of your SavingsAccount class

#initialize savings account object
s = SavingsAccount()
#call created account method
s.createAccount(name="a", initialDeposit=4)
#print the account
print(s.savingsAccounts[12345])

Although your datastructure is confusing, why not have one instance of a savings account object represent an individuals account? Then you could just assign member variables for values you want to track.

class SavingsAccount:
    def __init__(self, name, initial_deposit):
        self.account_name = name
        self.bal = initial_deposit
    def deposit(self, val):
        self.bal += val
    def return_account(self):
        return self.__dict__

Now you can use it more simplistically

s = SavingsAccount("name", 500)
s.deposit(500)
acc = s.return_account()
print(acc)
>> {"account_name": "name", "bal": 1000}

Upvotes: 1

Samuel O.D.
Samuel O.D.

Reputation: 392

The line that gives the error does this actions:

  1. Calls SavingsAccount.init() to create the object
  2. Asks for the item 12345 in the dictionary (that whas just created so it's empty)

You should structure your code in a different way. You should have a list of accounts or similar that is unique, and then insert in it the accounts you create.

Upvotes: 1

Related Questions