7

Is there a way to accomplish something like this? I work in Python, but I am not sure if there is a way to do it in any programming language...

class Parent():
 class_attribute = "parent"
 @staticmethod
 def class_method():
 print __class__.class_attribute
class Child(Parent):
 class_attribute = "child"

I know I can't call __class__ directly. Its just an example, because I would want something like reference to the class itself, because I want the child class to act differently based on its class_attribute.

And then supposed output should be like this:

> Parent.class_method()
"parent"
> Child.class_method()
"child"

I know the same technique can be accomplish through the instances. But I don't want to create instances, because sometimes the code within the __init__ method could be long and demanding and if I would want to call class_method often, I would have to create plenty of instances used just for this one method call. And because class_attribute and class_method are static and won't be changed by instances.

Kevin Burke
65.8k84 gold badges203 silver badges328 bronze badges
asked Feb 6, 2012 at 20:06
1
  • are you talking about simply static functions? Commented Feb 6, 2012 at 20:09

2 Answers 2

10

Er, sounds like you want a classmethod, which not surprisingly is done with the classmethod decorator:

class Parent(object):
 class_attribute = "parent"
 @classmethod
 def class_method(cls):
 print cls.class_attribute
class Child(Parent):
 class_attribute = "child"
>>> Parent.class_method()
parent
>>> Child.class_method()
child

Or, as bgporter points out, you can do it directly with the attributes, with no need for the methods at all.

answered Feb 6, 2012 at 20:12
Sign up to request clarification or add additional context in comments.

4 Comments

but considering the documentation: "if a class method is called for a derived class, the derived class object is passed as the implied first argument", does it mean that derived class object will be created any way? Because OP says that would like to avoid instance constructon.
I wanted to call the method, because the code in method might actually do something more complicated then just printing attributes. I though making an easier exmaple would make easier explanation of what I wanted... so @classmethod is a solution
@Tigran no. A class is itself an object (an instance of its metaclass, actually).
@DanielRoseman: ah ok, that was actually what I was thinking of. +1
4

It just works in Python, with or without creating instances:

>>> class Parent(object):
... attribute = "parent"
... 
>>> class Child(Parent):
... attribute = "child"
... 
>>> p = Parent()
>>> p.attribute
'parent'
>>> c = Child()
>>> c.attribute
'child'
>>> Parent.attribute
'parent'
>>> Child.attribute
'child'
>>> 
answered Feb 6, 2012 at 20:11

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.