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 893483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:13:46+00:00 2026-05-15T14:13:46+00:00

I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever

  • 0

I have a class like:

class MyClass:
     Foo = 1
     Bar = 2

Whenever MyClass.Foo or MyClass.Bar is invoked, I need a custom method to be invoked before the value is returned. Is it possible in Python? I know it is possible if I create an instance of the class and I can define my own __getattr__ method. But my scnenario involves using this class as such without creating any instance of it.

Also I need a custom __str__ method to be invoked when str(MyClass.Foo) is invoked. Does Python provide such an option?

  • 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-15T14:13:46+00:00Added an answer on May 15, 2026 at 2:13 pm

    __getattr__() and __str__() for an object are found on its class, so if you want to customize those things for a class, you need the class-of-a-class. A metaclass.

    class FooType(type):
        def _foo_func(cls):
            return 'foo!'
    
        def _bar_func(cls):
            return 'bar!'
    
        def __getattr__(cls, key):
            if key == 'Foo':
                return cls._foo_func()
            elif key == 'Bar':
                return cls._bar_func()
            raise AttributeError(key)
    
        def __str__(cls):
            return 'custom str for %s' % (cls.__name__,)
    
    class MyClass(metaclass=FooType):
        pass
    
    # # in python 2:
    # class MyClass:
    #    __metaclass__ = FooType
    
    
    print(MyClass.Foo)
    print(MyClass.Bar)
    print(str(MyClass))
    

    printing:

    foo!
    bar!
    custom str for MyClass
    

    And no, an object can’t intercept a request for a stringifying one of its attributes. The object returned for the attribute must define its own __str__() behavior.

    Updated 2023-02-20 for Python 3.x default implementation (python 2 as a comment).

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

Sidebar

Related Questions

Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have a class like this: private class MyClass { [DisplayName(Foo/Bar)] public string FooBar
If I have a class like this: class myClass { string foo; string bar;
If I have a class defined as classdef myclass properties foo = 3; bar
i have a class like: class myclass{ Long id; String a; Text b; }
I have a class like this: class MyClass{ public: MyClass(int Mode); private: std::map <
I have a base class like this: package MyClass; use vars qw/$ME list of
i have a class like [Serializable] public class MyClass { [XmlAttribute] public bool myBool
If I have a simple class setup like this: class MyClass { private string
I have a class that looks like the following: public class MyClass { private

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.