user1971598
user1971598

Reputation:

Nested classes and static method

I have this code:

class Servicer(object):
    clsVrb = "run"

    class SrvOne(object):
        def __init__(self, name):
            self.name = name

    class SrvTwo(object):
        def __init__(self, name):
            self.name = name

    @staticmethod
    def make_SrvOne(name):
        return SrvOne(name)





test = Servicer.make_SrvOne("Edgar")
print test

But I get an exception that SrvOne is undefined. How can it be undefined? Why doesn't Servicer see SrvOne?

Upvotes: 0

Views: 149

Answers (1)

Pavel Anossov
Pavel Anossov

Reputation: 62948

It's defined in the Servicer namespace, there's no local SrvOne in make_SrvOne and there is no global SrvOne.

@staticmethod
def make_SrvOne(name):
    return Servicer.SrvOne(name)

Why isn't Servicer just a module?

Upvotes: 5

Related Questions