Pergunta

Newbie here, so please be gentle. I am working with meshes and I have a set of immutable lists (tuples) which contain the attributes of an original input mesh. These lists, as well as the original mesh, are used as an organizational guide to keep tabs on newly created submeshes; i.e. since the submeshes reference the original mesh, its attributes (the lists) can be used to reference between submeshes, or to create submeshes of submeshes. Bellow are listed the structure of some of these lists:

vertices = [ (coordinate of vertex 1), ... ]
faceNormals = [ (components of normal of face 1), ...]
faceCenters = [ (coordinates of barycenter of face 1), ...]

Since I am not acquainted with oop, I have organized my script like so:

def main():
    meshAttributes = GetMeshAttributes()
    KMeans(meshAttributes, number_submeshes, number_cycles)

def GetMeshAttributes()
def Kmeans():
    func1
    func2
    ...
def func1()
def func2()
...
main()

The problem is that on every function inside KMeans, I have to pass some of the mesh's attributes as arguments, and I can't default them because they are unknown at the beginning of the script. For example inside Kmeans is a function called CreateSeeds:

def CreateSeeds(mesh, number_submeshes, faceCount, vertices, faceVertexIndexes):

The last three arguments are static, but I can't do something like:

CreateSeeds(mesh, number_submeshes)

because I would have to place faceCount, vertices, faceVertexIndexes inside the function definition, and these list are huge and unknown at the beginning.

I have tried using classes, but in my limited knowledge of them, I had the same problem. Could someone give me some pointers on where to look up a solution?

Thanks!

Foi útil?

Solução

What you want is to obtain a partial function application:

>>> from functools import partial
>>> def my_function(a, b, c, d, e, f):
...     print(a, b, c, d, e, f)
... 
>>> new_func = partial(my_function, 1, 2, 3)
>>> new_func('d', 'e', 'f')
1 2 3 d e f

If you want to specify the last parameters you can either use keyword arguments or a lambda:

>>> new_func = partial(my_function, d=1, e=2, f=3)
>>> new_func('a', 'b', 'c')
a b c 1 2 3
>>> new_func = lambda a,b,c: my_function(a, b, c, 1, 2, 3)
>>> new_func('a', 'b', 'c')
a b c 1 2 3
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top