Joonho Park
Joonho Park

Reputation: 563

In python, is there possible way to control flow using predefined variable such as #defined in C

script such as python and high level language such as C looks different in this behavior but I want to know there is possible way or other way to realize that.

In my python script, in one point of view, I want to see the procedure of Monte-Carlo simulation to see what is happening and in the other point, I want to run the dynamics and see the final result at the end. At the moment it is possible by changing some parts as below. But if I am going to use two different main.py's it would be very cumbersome because whenever you change something in a file, you need to change the same part in the other file. The present method is as below. This is the structure of animation script which shows every frame during simulation

import python_animation_modules
import ...

def init():
  bla, bla
  return lines,

def simulation():
  globals
  bla, bla
  set_lines() - redefine data using set_data
  return lines,

animation.FuncAniaion(..frames=Nstep..)

plt.show()

I can change this to show the last figure.

def init():
  bla, bla
  return lines,

while (i<Nax_num):
#def simulation():
#  globals
  bla, bla
  set_lines()       # redefine data using set_data
  #return lines,

#animation.FuncAniation(...MAX_num.)
plt.show()

Is there possible way such as C using preprocessor or other delicate technic in python. In C, this is possible,

#define DYNAMICS

#ifndef DYNAMICS
while( i< MAX_num):
#else
def simulation():
#  globals

Upvotes: 1

Views: 89

Answers (1)

pjz
pjz

Reputation: 43057

You seem to be wanting to use #IFDEF to keep from repeating code... but why not do it explicitly using code? something like:

def simulate():
    bla, bla
    set_lines()

def simulation():
    globals
    simulate()
    return lines,

def dynamics():
    animation.FuncAniation(...MAX_num.)

def no_dynamics():
    while (i < MAX_num):
        simulate()

if DYNAMICS: dynamics()
else: no_dynamics()
plt.show()

The root idea is: factor out the common bits into its own routine, then call that from wrappers that contain case-specific code.

Upvotes: 1

Related Questions