[英]not able to print instance using def __str__ , in a class?
我正在學習Python,目前正在學習課程。 我無法打印類的實例,代碼如下。
class CreditCard:
""" This is properly intended, but CNTRL+K resulted in unindentation
(stackoverflow's cntrl+k)"""
def __init__(self,customer,bank,account,limit):
""" Initializing the variables inside the class
Setting the Initial Balance as Zero
Customer : Name of the Customer
bank : Name of the Bank
balance : will be zero, initial
account : accoount number or identifier, generally a string
limit : account limit/ credit limit
"""
self.customer = customer
self.bank = bank
self.accnt=account
self.limit = limit
self.balance = 0
def get_customer(self):
""" returns the name of the customer """
return self.customer
def get_bank(self):
""" returns the Bank name """
return self.bank
def get_account(self):
""" returns the Account Number """
return self.account
def get_limit(self):
""" returns the Credit Limit """
return self.limit
def get_balance(self):
""" returns the Balance """
return self.balance
def charge(self,price):
""" swipe charges on card, if sufficient credit limit
returns True if transaction is processed, False if
declined """
if price + self.balance > self.limit:
return False
else:
self.balance=price+self.balance
# abve can be written as
# self.balance+=price
return True
def make_payment(self,amount):
""" cust pays money to bank, that reduces balance """
self.balance = amount-self.balance
# self.balance-=amount
def __str__(self):
""" string representation of Values """
return self.customer,self.bank,self.account,self.limit
我會毫無錯誤地運行它。 我創建了一個實例,
cc=CreditCard('Hakamoora','Duesche',12345678910,5000)
這就是我得到的。
>>> cc
<__main__.CreditCard instance at 0x0000000002E427C8>
我應該包括什么才能使其打印實例,例如
>>cc=CreditCard('Hakamoora','Duesche',12345678910,5000)
>>cc
>>('Hakamoora','Duesche',12345678910,5000)
請使用較少的技術術語(此處為新手)
pastebinlink: https : //paste.ee/p/rD91N
也嘗試過這些
def __str__(self):
""" string representation of Values """
return "%s,%s,%d,%d"%(self.customer,self.bank,self.account,self.limit)
和
def __str__(self):
""" string representation of Values """
return "({0},{1},{2},{3})".format(self.customer,self.bank,self.account,self.limit)
謝謝,
6er
您正在混淆__str__
和__repr__
。 考慮以下類別:
class Test(object):
def __str__(self):
return '__str__'
def __repr__(self):
return '__repr__'
您可以看到在以下位置調用了哪種方法:
>>> t = Test()
>>> t
__repr__
>>> print(t)
__str__
>>> [1, 2, t]
[1, 2, __repr__]
>>> str(t)
'__str__'
>>> repr(t)
'__repr__'
另外,請確保這兩個方法都返回字符串。 您當前正在返回一個元組,這將導致出現如下錯誤:
TypeError: __str__ returned non-string (type tuple)
三點:
(1)確保__str__
定義的縮進級別是CreditCard類的一種方法。 當前,它似乎是一個在 charge()
內部局部定義的函數,因此可能無法作為實例方法訪問(但是很難確定: charge()
本身及其其他方法也被縮進了。)
(2)在__str__
,返回一個字符串,而不是一個元組:
def __str__(self):
""" string representation of Values """
return str( ( self.customer,self.bank,self.account,self.limit ) )
(3)定義一個附加的__repr__
方法:當顯示帶有
>>> cc
而__str__
僅在有人(例如print
)試圖將對象強制為str
。 這是一個最小的示例:
def __repr__( self ): return str( self )
您忘記了將對象變成字符串(或打印它)。
請嘗試:
print(cc)
要么
str(cc)
文件是否確實縮進正確? 最后兩種方法(make_payment和__str__)縮進為好像它們是“收費”方法的一部分。
我在系統上對此進行了測試,並且這兩種方法(尤其是__str__)的縮進引起了與您相同的錯誤。 刪除縮進使我可以按所需方式打印“ cc”變量。
這是更正后的代碼,我今天已經學到了一個很棒的概念,了解了repr和str 。
class CreditCard:
""" Just a Normal Credit Card """
def __init__(self,customer,bank,account,limit):
""" Initializing the variables inside the class
Setting the Initial Balance as Zero
Customer : Name of the Customer
bank : Name of the Bank
balance : will be zero, initial
account : accoount number or identifier, generally a string
limit : account limit/ credit limit
"""
self.customer = customer
self.bank = bank
self.account=account
self.limit = limit
self.balance = 0
def get_customer(self):
""" returns the name of the customer """
return self.customer
def get_bank(self):
""" returns the Bank name """
return self.bank
def get_account(self):
""" returns the Account Number """
return self.account
def get_limit(self):
""" returns the Credit Limit """
return self.limit
def get_balance(self):
""" returns the Balance """
return self.balance
def charge(self,price):
""" swipe charges on card, if sufficient credit limit
returns True if transaction is processed, False if
declined """
if price + self.balance > self.limit:
return False
else:
self.balance=price+self.balance
# abve can be written as
# self.balance+=price
return True
def make_payment(self,amount):
""" cust pays money to bank, that reduces balance """
self.balance = amount-self.balance
# self.balance-=amount
def __str__(self):
""" string representation of Values """
return str((self.customer,self.bank,self.account,self.limit))
非常感謝
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.