简体   繁体   中英

python class doesn't work for different variable

dear expert i am trying to write a simulation and in my code i have a class like this:

... (some def are here)

    class multipole:
     global xxp,yyp,zzp,x,y,z,xp,yp,zp,t,tm,h
     xxp,yyp,zzp,x,y,z,xp,yp,zp =xxpyypzzp()  # some initial values calling
     @staticmethod    
     def quad(f1, f2,f3):
       global t,h,mass,ksimax 
       while t < ksimax:
         rk4_two(t,h,mass, f1, f2, f3, xxp, yyp, zzp) # rk function for new xxp, yyp and zzp 
         t = t + h
         tm.append(t)
         xp.append(xxp[1])
         x.append(xxp[0])
         yp.append(yyp[1])
         y.append(yyp[0])
         zp.append(zzp[1])
         z.append(zzp[0])
       return xp, x, yp,y,zp,z,tm
    if __name__ == "__main__":
     qp=multipole()
     quxp, qux, quyp,quy,quzp,quz,qutm=qp.quad(0.,0.,0.)
     hxp, hx, hyp,hy,hzp,hz,htm =qp.quad(0.022,0.,0.)
     oxp, ox, oyp,oy,ozp,oz,otm =qp.quad(-0.023,-0.032,0.0 )

my question is this code only calculate (quxp, qux, quyp,quy,quzp,quz,qutm), but not others (others will turn same value of quxp, qux, quyp,quy,quzp,quz,qutm) could you please tell me why? i am new in python any comments will be appreciated.

Ignoring the fact that this code is... somewhat flawed. I think that the problem is that you are using t which is apparently global but you don't reset it anywhere - so this loop:

while t < ksimax:
    ...

Will only run once, unless you reset t somewhere. Some pseudo code to explain why this happens:

counter = 0

def do_something():
    global counter
    print "Starting at", counter
    while counter <= 10:
        print counter
        counter += 5
    print "Done"

do_something()
# Starting at 0
# 0
# 5
# 10
# Done

do_something()  # Called again, the counter is at 10 now:
# Starting at 10
# Done

As others have mentioned, your code could benefit from some heavy refactoring. Some starting points:

  • Naming! What does xxpyypzzp even mean? Even if it's obvious to you today, it must be hard to read even for you and unless you have Rainman-like memory you will not understand this next week. Try using descriptive names and if you find yourself adding prefixes or suffixes to variables because you run out of names - think about encapsulating some of this complexity in a class. It seems like the suffixes xp , x , yp , y , zp , z and tm are used a lot. At least create a named tuple to hold these values.
  • Global variables is generally considered harmful . They make it hard to reason about code - which is why you got this bug in the first place. If you are sprinkling global statements over your code there is time to redesign it. Think about which part of your code should "own" which parts of the state.
  • Python has a coding standard, called PEP8 - read it and try to follow it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM