私はこの問題の答えを探しましたが、答えが見つからない、あまりにも具体的ではないかもしれません。オブジェクトに属性がありません - Pythonクラスの実行
私は簡単なプログラム、私の最初の適切なプログラムを持っていると私は練習として、主にそれを作成しました:私はすべてのprint文をコメント解除し、それらを同時に実行すると
import math
class Logs(object):
def __init__(self,a,b):
self.a = a
self.b = b
def apply_log(self):
self.a_log = math.log10(self.a)
self.b_log = math.log10(self.b)
return (self.a_log, self.b_log)
def add_log(self):
self.log_add = self.a_log + self.b_log
return self.log_add
def log_split(self):
self.log_c = self.log_add // 1
self.log_m = self.log_add % 1
return(self.log_c, self.log_m)
def result(self):
self.ex_m = 10 ** self.log_m
self.ex_v = 10 ** self.log_c
self.log_res = self.ex_m * self.ex_v
return self.log_res
lg = Logs(34,54)
#print(lg.apply_log())
#print(lg.add_log())
#print(lg.log_split())
print(lg.result())
プログラムが完全に実行されます。それぞれの結果をプリントアウトするとき、それがうまくいく理由を私は理解していない
Traceback (most recent call last):
File "python", line 33, in <module>
File "python", line 24, in result
AttributeError: 'Logs' object has no attribute 'log_m'
:私はちょうどインスタンスの結果を印刷し、他の三つのprint文をコメントアウトしたい場合は、それがエラーをスローしますまたはなぜこれがプログラムの実行方法に影響を与えるかを示します。
私はクラスを使用したのは初めてです(プログラムのポイントはクラスを作成するための練習でした)ので、エラーは私が作成した方法であると想像します。
ご協力いただければ幸いです。それはlog_m
の方法log_split
に初期化方法result
で使用されているためAttributeError
をスローする必要があります
おかげ
'self.log_m'が作成される前に' lg.result() 'を呼び出しています。コメントアウトしたプリントを削除しても問題ありません。 – MooingRawr