2015-05-25 58 views
-3

在执行这个我收到提示说:Python对象()不带参数

D:\SARFARAZ\Python>Python assignment_01.py 
Traceback (most recent call last): 
    File "assignment_01.py", line 35, in <module> 
    BankAcc1 = BankAcc('Sarfaraz',12345,50000) 
TypeError: object() takes no parameters 

代码:

#Assignment_01 : Class Creation 
class BankAcc(object): 
    int_rate = 0.7 
    balance = 0 

    #def **_init_**(self, name, number, balance): --> error was in this line, I've corrected it now as below 
    def __init__(self, name, number, balance): 
     self.name = name 
     self.number = number 
     self.balance = balance 

    def withdraw(self, amount): 
     self.balance -= amount 
     return self.balance 

    def deposit(self, amount): 
     self.balance += amount 
     return self.balance 

    def add_interest(self): 
     self.interest = int_rate * self.balance 
     self.balance += self.interest 
     return self.balance 

'''class MinimumBalanceAccount(BankAccount): 
    def __init__(self, min_bal): 
     BankAccount.__init__(self) 
     self.min_bal = 500 

    def withdraw(self, amount): 
     if self.balance - amount < self.min_bal: 
      print ("Sorry, minimum balance must be maintained.") 
     else: 
      BankAccount.withdraw(self, amount)''' 

BankAcc1 = BankAcc('Sarfaraz',12345,50000) 
BankAcc2 = BankAcc1.withdraw(1000) 
print (BankAcc2) 

我试图创建一个对象,然后试图调用退出()方法 和撤回一些金额后打印余额。

回答

2

您的__init__方法名错字:

def _init_(self, name, number, balance): 

您需要使用强调在开始和结束。

如果没有有效命名的__init__方法,Python会回退到object.__init__,该参数不带参数。

+0

我怎么会这么傻,错过了.. !!!感谢@Martijn Pieters – sarfarazit08