Rahul Sharma
Rahul Sharma

Reputation: 1169

Is it possible to have a function within a method in Python?

If so, how would you do this.

def methodname(self, blah, blah) ^how do I place a function inside this.

Upvotes: 0

Views: 89

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Yes, you just stick a def inside. You can even nest whole classes inside functions and methods:

class Foo(object):
    def method(self, bar):
        def inner(magic):
            class Madness(object):
                def __init__(inself, foo): inself.foo = foo
            return Madness(magic)
        return inner(bar)

Upvotes: 4

Related Questions