Python类对象在for循环中没有属性。

huangapple go评论73阅读模式
英文:

Python class object has no attribute in a for loop

问题

I have a Monte Carlo simulation where I am trying to randomize my Dryden wind model class with each iteration. Initializing the class works as expected for the first iteration; however, future iterations have the error "AttributeError: 'dryden' object has no attribute 'dryden'."

我有一个蒙特卡洛模拟,我尝试在每次迭代中随机化我的Dryden风模型类。初始化该类在第一次迭代中按预期工作,但在未来的迭代中出现错误:"AttributeError: 'dryden'对象没有'dryden'属性"。

I am using this basic code structure, with the working aspects omitted. I don't believe the normal distribution of the velocity is an issue, as I get the same error with only the constant value too.

我使用这个基本的代码结构,省略了工作方面的部分。我不认为速度的正态分布是问题,因为即使只使用常数值,我也会得到相同的错误。

for j in tqdm(range(0, iterations)):
# Dryden initialization
    dryden_init = 1
    dryden_scale = 2

    if dryden_init == 1:
        dryden = dryden.dryden(np.random.normal(initlibrary.UAV_0["u0"], 5), alt0, dur, dt)
        y1_f, y2_f, y3_f = dryden.dryden_wind_velocities()

Inside the 'dryden' class, I have an initialization that is usually called with 'dryden.dryden' seen above. I think this is the part that is not working.

在'dryden'类内部,我有一个通常使用上面的'dryden.dryden'调用的初始化。我认为这是不起作用的部分。

class dryden:
    def __init__(self, Vgps, alt_ref, dur, dt):
        self.Vgps = Vgps
        self.alt_ref = alt_ref
        self.dur = dur
        self.dt = dt

I am a newer Python user and couldn't find anything online related to this issue, but I don't know what to search. Any help is appreciated!

我是一个较新的Python用户,但在网上找不到与此问题相关的信息,我不知道该搜索什么。任何帮助都将不胜感激!

英文:

I have a Monte Carlo simulation where I am trying to randomize my Dryden wind model class with each iteration. Initializing the class works as expected for the first iteration; however, future iterations have the error "AttributeError: 'dryden' object has no attribute 'dryden'."

I am using this basic code structure, with the working aspects omitted. I don't believe the normal distribution of the velocity is an issue, as I get the same error with only the constant value too.

for j in tqdm(range(0, iterations)):
# Dryden initialization
    dryden_init = 1
    dryden_scale = 2

    if dryden_init == 1:
        dryden = dryden.dryden(np.random.normal(initlibrary.UAV_0["u0"], 5), alt0, dur, dt)
        y1_f, y2_f, y3_f = dryden.dryden_wind_velocities()

Inside the 'dryden' class, I have an initialization that is usually called with 'dryden.dryden' seen above. I think this is the part that is not working.

class dryden:
    def __init__(self, Vgps, alt_ref, dur, dt):
        self.Vgps = Vgps
        self.alt_ref = alt_ref
        self.dur = dur
        self.dt = dt

I am a newer Python user and couldn't find anything online related to this issue, but I don't know what to search. Any help is appreciated!

答案1

得分: 1

在这一行:

dryden = dryden.dryden(...

你将 dryden 从你最初导入的类类型更改为该类的一个实例。请在这个循环中为变量选择一个唯一的名称:

dryden_instance = dryden.dryden(np.random.normal(initlibrary.UAV_0["u0"], 5), alt0, dur, dt)
y1_f, y2_f, y3_f = dryden_instance.dryden_wind_velocities()
英文:

On this line

dryden = dryden.dryden(...

You change dryden from the class type you originally imported to instead an instance of that class. Instead pick a unique name for the variable in this loop

dryden_instance = dryden.dryden(np.random.normal(initlibrary.UAV_0["u0"], 5), alt0, dur, dt)
y1_f, y2_f, y3_f = dryden_instance.dryden_wind_velocities()

huangapple
  • 本文由 发表于 2023年6月22日 01:13:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定