import numpy as np
class FourVector:
""" This document is a demonstration of how to create a class of Four vector """
def __init__(self,ct=0,x=0,y=0,z=0):
self.a=(ct,x,y,z)
self.r=(ct,r=[x,y,z])
P0 = FourVector()
print P0.a
P1 = FourVector(ct=9,x=1,y=2,z=4)
print P1.a
P2 = FourVector(ct=99.9,r=[1,2,4])
我的代码适用于P0
,P1
但不适用于P2
:(任何人都可以发现我的错误吗?
答案 0 :(得分:2)
r
甚至不在参数列表中,为什么?只需添加它:
def __init__(self,ct=0,x=0,y=0,z=0, r=None)
答案 1 :(得分:2)
您的r
方法中没有__init__
参数:
class FourVector:
def __init__(self, ct = 0, x = 0, y = 0, z = 0, r = None):
self.a = (ct, x, y, z)
if r is not None:
self.a = (ct, r[0], r[1], r[2])
P0 = FourVector()
print P0.a
P1 = FourVector(ct = 9, x = 1, y = 2, z = 4)
print P1.a
P2 = FourVector(ct = 99.9, r = [1, 2, 4])
print P2.a
答案 2 :(得分:1)
import numpy as np
class FourVector:
""" This document is a demonstration of how to create a class of Four vector """
def __init__(self, ct=0, x=0, y=0, z=0, r=[]):
self.ct = ct
self.r = np.array(r if r else [x,y,z])
P0 = FourVector()
print P0.r
P1 = FourVector(ct = 9, x = 1, y = 2, z = 4)
print P1.r
P2 = FourVector(ct = 99.9, r = [1, 2, 4])
print P2.r