[英]python @classmethod returning 'nonetype'
這里是 python 的新手,並且仍在經歷一些成長的痛苦。 由於糟糕的源代碼控制管理,我丟失了使此類方法工作的原始代碼。 我的目的是讓類方法從用戶那里獲取輸入並將這些輸入分配給屬性以創建一個對象。
class User:
def __init__(self, date, name, phone, shells, taps):
self.date = date
self.name = name
self.phone = phone
self.shells = shells
self.taps = taps
self.total = (shells * 100) + (taps * 50)
swaps = []
self.swaps = swaps
@classmethod
def get_info(cls):
self.date = input('Enter the date: ')
self.name = input('Enter the full name: ')
self.phone = input("Enter the phone number: ")
self.shells = input('Enter the number of shells: ')
self.taps = input('Enter the number of taps: ')
當我嘗試調用“新創建的對象”時,我收到屬性錯誤“nonetype”。 我覺得我很接近但還沒有得到正確的組合來讓它工作。 謝謝你的幫助!
get_info
不應該是User
一部分。 (下周您將需要根據來自套接字或數據庫的數據創建一個用戶..)
所以收集輸入並實例化一個User
實例。
見下文
class User:
def __init__(self, date, name, phone, shells, taps):
self.date = date
self.name = name
self.phone = phone
self.shells = shells
self.taps = taps
self.total = (shells * 100) + (taps * 50)
swaps = []
self.swaps = swaps
date = input('Enter the date: ')
name = input('Enter the full name: ')
phone = input("Enter the phone number: ")
shells = input('Enter the number of shells: ')
taps = input('Enter the number of taps: ')
user: User = User(date,name,phone,shells,taps)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.