0

Suppose I have a Class that's passed a set of options.

For example:

class Thing(object): 
    def __init__(self, options): 
        self.name = options['name']
        if options['material'] == 'organic': 
            self.living = options['living'] 
            if self.living: 
                self.kingdom = options['kingdom'] 
        else:
            self.material = options['material'] 

So if it's material and organic, it will have an additional option specifying if it's living, if it's living it will also have a kingdom etc.

Suppose I want to define a function in class Thing that will for example:

def belongToKingdom(self): 
    print self.kingdom

But I only want such a function defined at __init__ if kingdom is passed in options, otherwise ignored. What's the correct way to accomplish this ?

2
  • Have a subclass class LivingThing(Thing):? Commented Jul 7, 2014 at 8:20
  • Yeah I know, it was just supposed to be a simplified example, the actual use case is a little different because there is a set of functions that's dynamically generated from an array of paired values. So I can't just make subclasses for every possible combination of pairs, I actually need to take a standard prototype function and generate it with a few variables determined by the value pairs. Commented Jul 7, 2014 at 8:22

1 Answer 1

1
class Thing(object):
    def __init__(self, **options):
        if options.get('living',None):
            def belongToKingdom(self):
                print(self.kingdom)
            self.kingdom = options['kingdom']
            self.belongToKingdom = belongToKingdom
Sign up to request clarification or add additional context in comments.

6 Comments

This might be appropriate for a different question, but suppose I don't know what the function I want to define will be called is there a way to str format the function name? I.e. a working equivalent for self.belongTo{0}.format(options['kingdom']) <which would persumably always return True but anyways>
@user3467349 I can't really make sense of what you're asking, but it sounds like you're trying to put meaningful data in variable names. Don't do that! Just build a list of options instead, so Thing().options returns {'living':True, 'kingdom':"Animal", ... } and test for it
@user3467349 but of course, there is in fact a way to do just what you want. Instead of manipulating self, manipulate self.__dict__. Then you can use a string instead of a variable name. For example: self.__dict__["belongTo{}".format('kingdom')] = lambda self: print(self.kingdom) works (it's just uglier than sin)
Yes I was just thinking of that. I could also just make a new dictionary right i.e. self.printMyAttribute = {} and then add into it so it looks like {'kingdom': <function kingdom> } etc. Is there a reason you would discourage something like that?
@AdamSmith this won't work; belongToKingdom is a function, which isn't quite the same as a method. You need to from types import MethodType then make the assignment self.belongToKingdom = MethodType(belongToKingdom, self)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.