2

I have a class definition which defines a static method. I have a field which I would like to initialize with the static method. My default thinking led me to this:

class SomeConcreteClass(object):
    some_data = SomeConcreteClass.create_default_data()
    @staticmethod
    def create_default_data():
        return 'Foo'

The problem is that when I run this, I get a NameError: name 'SomeConcreteClass' is not defined. It makes sense as the SomeConcreteClass is just being built. Does this mean I cannot use static init functions? Is there an alternate way which is recommended to handle such a situation?

5
  • Will this class have any data that belongs to an instance? Commented Jan 7, 2013 at 16:56
  • 3
    The class name not being in scope isn't even the whole problem; you're also trying to call a method that hasn't been defined yet. Commented Jan 7, 2013 at 16:56
  • @Wooble: True, but if you put the call at the end the problem persists. Commented Jan 7, 2013 at 16:57
  • I understand that SomeConcreteClass is not defined. The function could be placed before where it is first used, but it would not solve this issue. My thought right now is to make some_data a @property instead of it being a field which is initialized at class def time. Really, I am looking on style guidance here. Commented Jan 7, 2013 at 16:57
  • what about adding (immediatly) after the class-def: SomeConcreteClass.some_data = SomeConcreteClass.create_default_data() ?? Commented Jan 7, 2013 at 17:40

3 Answers 3

3

The appropriate place for create_default_data would be outside the class entirely. Then your problems go away:

def create_default_data():
    return 'Foo'

class SomeConcreteClass(object):
    some_data = create_default_data()

If you really do want it as a static method inside the class that's alright too:

def _create_default_data():
    return 'Foo'

class SomeConcreteClass(object):
    some_data = _create_default_data()
    create_default_data = staticmethod(_create_default_data)

but static methods aren't often used in Python because there's no need to put a function inside a class unless it operates on the class in some way.

Sign up to request clarification or add additional context in comments.

Comments

1

If some_data is exactly the output of create_default_data (and assuming the latter is deterministic in the context of your call) then why not just make some_data a @property?

class SomeConcreteClass(object):
    @property
    def some_data():
        return 'Foo'

Alternatively, but not equivalently, you could initialize some_data for each instance within __init__.

2 Comments

or jsut make some_data a property--I went down this route.
@TheodrosZelleke Well, yes, but with the name some_data. :)
1

I don't think you want to do this. Don't forget that Python is not Java ™... attributes defined at class level are class attributes, not instance attributes. You almost certainly want the data to be instance-specific, so you should do this in the __init__ method. You can certainly call the classmethod from within that method, if you want to, or (better) just put the code in __init__.

1 Comment

In this case the data is actually not instance specific. It really is part of the definition of the class. I have simplified my example a bit, but in reality my SomeConcreteClass extends another class which has an __init__ which accepts params. I would prefer to not have to implement a pass-through __init__ in all of the classes which extend the parent. For now, I am solving the issue by making some_data a property, but on a purist level I feel it is a bit less than ideal as the data is not instance specific.

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.