Replace code with a variable Python -


i have program in part of code has modified constantly:

var = 'math.sin(x*y)*math.sin(x*y)'  while (x <=  vfinal) , (y <= vfinal):     v1 = bm.verts.new((round(x,3),round(y,3),var))     x = x + precision     v2 = bm.verts.new((round(x,3),round(y,3),var))     y = y + precision     x = x - precision     v3 = bm.verts.new((round(x,3),round(y,3),var))     x = x + precision     v4 = bm.verts.new((round(x,3),round(y,3),var))     bm.faces.new((v1,v2,v4,v3))     y = y - precision     if (round(x,1) == vfinal):         y = y + precision         x = vinicial 

since math.sin(x*y)*math.sin(x*y) appears 4 times (possibly more once expand program), want change program changing whats stored in var.

so far tried making var string, gives error because bm.verts.new wont accept strings. tried removing ' ' in var, make number, won't give desired result further down because x , y change constantly. thing worked writing math.sin(x*y)math.sin(xy) 4 times, tedious , im lazy.

is there way want? if not, should do?

rather trying dynamically execute code, can make var function:

var = lambda x, y: math.sin(x * y) * math.sin(x * y) 

or if prefer vanilla function:

def var(x, y):     return math.sin(x * y) * math.sin(x * y) 

you can reuse logic calling function. example:

v1 = bm.verts.new((round(x,3),round(y,3),var(x, y))) 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -