亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在類對象中引發“dtype”參數錯誤

如何在類對象中引發“dtype”參數錯誤

拉莫斯之舞 2023-06-27 13:09:08
我有以下代碼:import numpy as npclass circle(object):    def __init__(self, radius=3, color='blue', data_type=np.float64):   # default values        self.radius = radius        self.color = color         self.data_type = data_type            if self.data_type not in [np.float32, np.float64]:        raise ValueError('data_type should be np.float32 or np.float64 only')            def add_radius(self, r):        self.radius = self.radius + np.ceil(r, dtype=self.data_type)        return(self.radius)    redcircle = circle(radius=10, color='red', data_type=np.float32)redcircle.add_radius(2.323) 當我運行代碼時,出現以下錯誤:---------------------------------------------------------------------------NameError                                 Traceback (most recent call last)<ipython-input-39-27704fa7e57b> in <module>----> 1 class circle(object):      2     def __init__(self, radius=3, color='blue', data_type=np.float64):   # default values      3         self.radius = radius      4         self.color = color      5         self.data_type = data_type<ipython-input-39-27704fa7e57b> in circle()      5         self.data_type = data_type      6 ----> 7     if self.data_type not in [np.float32, np.float64]:      8         raise ValueError('dtype should be np.float32 or np.float64 only')      9 NameError: name 'self' is not defined如何在類對象中指定參數,以便在指定的參數值不是類對象所接受的值dtype時引發錯誤?dtype如果有人知道的話,非常感謝。
查看完整描述

1 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

Python 的結構完全由縮進級別控制:


class circle(object):

    def __init__(self, radius=3, color='blue', data_type=np.float64):   # default values

        self.radius = radius

        self.color = color 

        self.data_type = data_type

        # Python assumes this is the end of the __init__ function


    # because this next line is at the next higher level

    if self.data_type not in [np.float32, np.float64]:

        raise ValueError('data_type should be np.float32 or np.float64 only')

    

    # so the above is a fragment that will not run in __init__. 


    def add_radius(self, r):

        self.radius = self.radius + np.ceil(r, dtype=self.data_type)

        return(self.radius)

這是否回答你的問題?


查看完整回答
反對 回復 2023-06-27
  • 1 回答
  • 0 關注
  • 165 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號