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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:26:34+00:00 2026-06-10T21:26:34+00:00

I am trying to implement a class in which an attempt to access any

  • 0

I am trying to implement a class in which an attempt to access any attributes that do not exist in the current class or any of its ancestors will attempt to access those attributes from a member. Below is a trivial version of what I am trying to do.

class Foo:
    def __init__(self, value):
        self._value = value

    def __getattr__(self, name):
        return getattr(self._value, name)

if __name__ == '__main__':
    print(Foo(5) > Foo(4)) # should do 5 > 4 (or (5).__gt__(4))

However, this raises a TypeError. Even using the operator module’s attrgetter class does the same thing. I was taking a look at the documentation regarding customizing attribute access, but I didn’t find it an easy read. How can I get around this?

  • 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-06-10T21:26:35+00:00Added an answer on June 10, 2026 at 9:26 pm

    If I understand you correctly, what you are doing is correct, but it still won’t work for what you’re trying to use it for. The reason is that implicit magic-method lookup does not use __getattr__ (or __getattribute__ or any other such thing). The methods have to actually explicitly be there with their magic names. Your approach will work for normal attributes, but not magic methods. (Note that if you do Foo(5).__lt__(4) explicitly, it will work; it’s only the implicit “magic” lookup — e.g., calling __lt__ when < is used) — that is blocked.)

    This post describes an approach for autogenerating magic methods using a metaclass. If you only need certain methods, you can just define them on the class manually.

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

Sidebar

Related Questions

I'm trying to implement a class in C# with a method that intercepts the
I am trying to implement a template class that would be able to tell
Using import scala.collection.mutable.{Publisher, Subscriber} I'm trying to implement a class that subscribes to events
I am trying to implement abstract base entity class which has overriden equals and
i have a class which implement Serializable. when i'm adding an object from that
I'm trying to implement delegation for a class which should call it's delegate (if
I'm trying to implement the following: I have an Importer class which registers so
I'm trying to implement a class in a Silverlight 4 RIA Services application that
I'm trying to implement a distinctOn function for a sequence which will take a
I'm trying to implement class inheritance hieararchy with NHibernate and SQL server. I made

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.