-3

I need to create multiple instances of function def name_VARIABLE_NUMBER(self): under one class.

The number of instances will be determined from the input file and is unknown from the beginning. Is it possible to run something like this?

NUMBER_of_INSTANCES = 10

class test:
    def instance_1(self):
        print (str(1**3))

    def instance_2(self):
        print (str(2**3))
...
    def instance_10(self):
        print (str(9**3))

I want to create loop for making different function names under one class with option to call them with test.instance_NUMBER.

What is the best option to create multiple and unknown number of defs with different names inside class?

The initial code can be found here. It is the mayavi scenes. They will be read from input files - so, the number of scenes is unknown from the beginning. And name for the each scene should be different.

XuMuK
  • 564
  • 4
  • 11
  • 32
  • 1
    They are not instances, they are methods. But I am confused about what you're trying to do in a general sense. – roganjosh Jun 08 '18 at 16:34
  • 1
    Assuming you could do this, how would you use those functions? – melpomene Jun 08 '18 at 16:35
  • 5
    This appears to be an x-y problem. ***Why*** do you want to do this, instead of generalizing one function to handle the instance number as a function parameter. – Prune Jun 08 '18 at 16:36
  • @roganjosh OK, maybe I am mistaken with terminology - I do not know much about classes. In general I want to make some different functions under one class, but the number of functions will be different each time. – XuMuK Jun 08 '18 at 16:38
  • @melpomene they will be called from another function – XuMuK Jun 08 '18 at 16:40
  • @XuMuK I'm almost certain that this is a terrible idea to address whatever problem you're trying to overcome. Could you imagine if regular python builtins behaved like this? A class should have predictable behaviour, even if it's across a variety of scenarios, not the other way around. – roganjosh Jun 08 '18 at 16:41
  • 1
    "Called from another function" is not enough of a "how would you use this" to describe your problem. Please post your intended usage example. – Prune Jun 08 '18 at 16:41
  • @Prune is it possible to run `def name_i`? – XuMuK Jun 08 '18 at 16:42
  • Also, if you don't yet know much about classes, you should learn more before you attempt to design something convoluted. Also, when you see that you've mis-used a term, please look up the proper usage and correct your posting. – Prune Jun 08 '18 at 16:43
  • @XuMuK Show me the code that would call them. – melpomene Jun 08 '18 at 16:43
  • I'd like to tone back my last comment, I've re-read and it doesn't come across as I'd like. I think that the approach you are using might stem from a lack of knowledge in other ways to address the problem. If we know the problem you're trying to solve, we might be able to suggest a better way. – roganjosh Jun 08 '18 at 16:48
  • @melpomene I have added the link to the code - it is mayavi scenes. – XuMuK Jun 08 '18 at 16:48
  • @roganjosh it is the mayavi scenes from my last questions - I want to implement different scenes with option to switch between them – XuMuK Jun 08 '18 at 16:50
  • Stack Overflow works on posted code, not links to off-site repositories. Please read and follow the posting guidelines in the help documentation, as suggested when you created this account. – Prune Jun 08 '18 at 16:50
  • @Prune It's not even a repo, it's just a link to another question. – melpomene Jun 08 '18 at 16:50
  • @XuMuK I don't see any code that calls `instance_1()` there. – melpomene Jun 08 '18 at 16:51
  • @melpomene Thanks; I see now. – Prune Jun 08 '18 at 16:53
  • @XuMuK my down-vote and closure vote remain in place. It appears that you're trying to get SO to write your application for you. It appears that you've chosen an application that is currently far beyond your software skills. This question needs more help than we can provide. We like helping people, but sometimes you need to help yourself first by reading a book on the topic, the on-line documentation, or asking someone you know. Once you understand the topic better, come back with specific questions. – Prune Jun 08 '18 at 16:56
  • 1
    In the link you provided, the only useful code there is something that someone else wrote. Now you're asking us to adapt that to a domain of your choosing, with no significant contribution from you. ***You*** need to close your gap in understanding before it's reasonable to ask for help from SO. – Prune Jun 08 '18 at 16:57
  • @Prune I stuck on position where the number of scenes is predefined in code. I want to make it dynamic and the problem is in my lack of knowledge in class coding. Why I am asking the more simple example. I think Yserbius gave necessary link. – XuMuK Jun 08 '18 at 17:06
  • @Prune I totally agree, so why I asking simple question which won't be used directly, but gives the solution. I am not asking help to write all the code. Just one function - I couldn't find any tutorial by myself. And the similar question already was asked. If I had found it, I won't asked anything. – XuMuK Jun 08 '18 at 18:05

3 Answers3

2

Although it's possible to do with setattr or manipulating the __dict__ of a class, but in your use case, would it be a better option to create a single function with a parameter? which to me is more clear.

class test:
    def instance_n(self, n):
        print(str(n**3))
  • `instance_n` should be `instance_1, instance_2...` How to make the names of functions with variable numbers? – XuMuK Jun 08 '18 at 16:58
1

What you're asking is to dynamically create methods at runtime which is a portion of what the following question is asking too.

Dynamic/runtime method creation (code generation) in Python

In a way, a Python method can be like a class with some certain attributes. You can define these attributes at runtime by using the built-in setattr method which is designed to set the attributes of a class.

Yserbius
  • 1,375
  • 12
  • 18
  • This is a noobie question. What does it mean for a method to be generated at runtime? – eWizard Feb 04 '22 at 16:29
  • @eWizard In general, if you're going to call a method, like `foo.doSomething()` that method will be defined in the code somewhere, ie `def doSomething(self)`. Generating it at runtime means that there's no `def` in the code and the name of your method is generated while the code is running. – Yserbius Feb 07 '22 at 02:25
0

Got the working solution, thanks to the Yserbius

NUMBER_of_INSTANCES = 10

class test(object):
    pass

def add_instance(cls,i):
    def instance(self):
        print (str(i**3))
    instance.__doc__ = "docstring for instance%d" % i
    instance.__name__ = "instance%d" % i
    setattr(cls,instance.__name__,instance)

d = test()
for i in range(NUMBER_of_INSTANCES):
    add_instance(test, i)        
    a = str('d.instance'+str(i)+'()')
    exec(a)
XuMuK
  • 564
  • 4
  • 11
  • 32