gpt4 book ai didi

python - 使用 __init__ 为四个向量创建一个类

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:41 24 4
gpt4 key购买 nike

Four Vectors

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])

我的代码适用于 P0P1 但不适用于 P2 :( 谁能发现我的错误?

最佳答案

您的 __init__ 方法中没有 r 参数:

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

关于python - 使用 __init__ 为四个向量创建一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19943820/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com