Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7436337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:13:26+00:00 2026-05-29T10:13:26+00:00

Is there a way to accomplish something like this? I work in Python, but

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:13:26+00:00Added an answer on May 29, 2026 at 10:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was wondering if there was a way to accomplish this, or if there
I'm trying to determine if there is a way to accomplish this elegantly. I
Is there an easy (therefore quick) way to accomplish this? Basically just take some
I google it up but didn' find something like this. I am having an
is there way how to get name ov event from Lambda expression like with
There should be a simple Linq query for what I'm trying to accomplish, but
I'm trying to do a debug system but it seems not to work. What
I wonder if there is a good way to bind local variables in python.
I can check for a module in Python doing something like: try: import some_module
Is there any way to access a dynamically accessing member of a User- defined

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.