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

  • SEARCH
  • Home
  • 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 8734549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:51:46+00:00 2026-06-13T09:51:46+00:00

I’m defining a Debug class like so: _debug = False class Debug: DrawOutlines =

  • 0

I’m defining a Debug class like so:

_debug = False

class Debug:
    DrawOutlines = True
    InvinciblePlayer = True

I want to override the Debug class so that if _debug is False, any class attribute of Debug (that exists) would be False. What __function__ do I override in order to change how class attributes are accessed?

Edit:

I know that simply overriding __getattribute__ will not work for class attributes:

>>> _debug = False
False
>>> class Debug:
...     DrawOutlines = True
...
...     def __getattribute__(self, name):
...         return _debug and object.__getattribute__(self, name)
...
>>> Debug.DrawOutlines
True
>>> Debug.cake
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'Debug' has no attribute 'cake'

Would this be a case where I would need a metaclass?

  • 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-13T09:51:47+00:00Added an answer on June 13, 2026 at 9:51 am

    Yes, you need a metaclass, because if you define __getattribute__ in class Debug (which, note, must be a new-style class), Python will invoke that for attribute lookups against instances of Debug, but not for attribute lookups against Debug itself.

    That makes sense, because Debug.__getattribute__ is defined to operate on instances of Debug, not the class Debug. (You could imagine defining a classmethod __getattribute__, but I can’t find any evidence that Python has any machinery that would invoke such a thing.)

    The first thing I thought of here is to add another __getattribute__ where Python would look for it for Debug class attribute lookups, namely, in the class of which the Debug class is an instance: Debug.__class__.__getattribute__.

    This does exist and works as you would expect:

    >>> Debug.__class__.__getattribute__(Debug, 'Draw')
    True
    >>> Debug.__class__.__getattribute__(Debug, 'Bogus')
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<stdin>", line 4, in __getattribute__
    AttributeError: 'DebugType' object has no attribute 'Bogus'
    

    But it’s not modifiable:

    >>> Debug.__class__.__getattribute__ = Debug.__getattribute__
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: can't set attributes of built-in/extension type 'type'
    

    This seems to be just a fact of life with the Python implementation; while the concept exists, you’re not allowed to modify attributes of builtin types so this approach won’t work.

    However, metaclasses to the rescue. You probably already know how to do this, but for other readers I’ll give an example (and there’s another example in this answer, to which my answer owes some debt).

    _debug = True
    >>> class DebugType(type):
    ...     def __getattribute__(self, name):
    ...             print 'attr lookup for %s' % str(name)
    ...             return _debug and object.__getattribute__(self, name)
    ... 
    >>> class Debug(object):
    ...     Draw = True
    ...     __metaclass__ = DebugType
    ... 
    >>> _debug
    False
    >>> Debug.Draw
    attr lookup for Draw
    False
    >>> _debug = True
    >>> Debug.Draw
    attr lookup for Draw
    True
    

    So to boil it down, the default class implementation for classes is type, so the default attribute looker-upper for class attributes is type.__getattribute__, and you cannot modify or replace type.__getattribute__ directly, but you can replace type using the metaclass mechanism, and, as in the example above, replace it with a subclass of type that has the __getattribute__ you want.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a small JavaScript validation script that validates inputs based on Regex. I
For some reason, after submitting a string like this Jack’s Spindle from a text

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.