Can I override a class function without creating a new class in Python?

前端 未结 3 1434
我寻月下人不归
我寻月下人不归 2021-02-08 13:10

I\'m making a game in pygame and I have made an \'abstract\' class that\'s sole job is to store the sprites for a given level (with the intent of having these level objects in a

3条回答
  •  星月不相逢
    2021-02-08 13:41

    Yes, it's doable. Here, I use functools.partial to get the implied self argument into a regular (non-class-method) function:

    import functools
    
    class WackyCount(object):
        "it's a counter, but it has one wacky method"
        def __init__(self, name, value):
            self.name = name
            self.value = value
        def __str__(self):
            return '%s = %d' % (self.name, self.value)
        def incr(self):
            self.value += 1
        def decr(self):
            self.value -= 1
        def wacky_incr(self):
            self.value += random.randint(5, 9)
    
    # although x is a regular wacky counter...
    x = WackyCount('spam', 1)
    # it increments like crazy:
    def spam_incr(self):
        self.value *= 2
    x.incr = functools.partial(spam_incr, x)
    
    print (x)
    x.incr()
    print (x)
    x.incr()
    print (x)
    x.incr()
    print (x)
    

    and:

    $ python2.7 wacky.py
    spam = 1
    spam = 2
    spam = 4
    spam = 8
    $ python3.2 wacky.py    
    spam = 1
    spam = 2
    spam = 4
    spam = 8
    

    Edit to add note: this is a per-instance override. It takes advantage of Python's attribute look-up sequence: if x is an instance of class K, then x.attrname starts by looking at x's dictionary to find the attribute. If not found, the next lookup is in K. All the normal class functions are actually K.func. So if you want to replace the class function dynamically, use @Brian Cane's answer instead.

提交回复
热议问题