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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:30:23+00:00 2026-05-26T01:30:23+00:00

It is pretty easy to implement __len__(self) method in Python so that it handles

  • 0

It is pretty easy to implement __len__(self) method in Python so that it handles len(inst) calls like this one:

class A(object):

  def __len__(self):
    return 7

a = A()
len(a) # gives us 7

And there are plenty of alike methods you can define (__eq__, __str__, __repr__ etc.).
I know that Python classes are objects as well.

My question: can I somehow define, for example, __len__ so that the following works:

len(A) # makes sense and gives some predictable result
  • 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-26T01:30:23+00:00Added an answer on May 26, 2026 at 1:30 am

    What you’re looking for is called a “metaclass”… just like a is an instance of class A, A is an instance of class as well, referred to as a metaclass. By default, Python classes are instances of the type class (the only exception is under Python 2, which has some legacy “old style” classes, which are those which don’t inherit from object). You can check this by doing type(A)… it should return type itself (yes, that object has been overloaded a little bit).

    Metaclasses are powerful and brain-twisting enough to deserve more than the quick explanation I was about to write… a good starting point would be this stackoverflow question: What is a Metaclass.

    For your particular question, for Python 3, the following creates a metaclass which aliases len(A) to invoke a class method on A:

    class LengthMetaclass(type):
    
        def __len__(self):
            return self.clslength()
    
    class A(object, metaclass=LengthMetaclass):
    
        @classmethod
        def clslength(cls):
            return 7
    
    print(len(A))
    

    (Note: Example above is for Python 3. The syntax is slightly different for Python 2: you would use class A(object):\n __metaclass__=LengthMetaclass instead of passing it as a parameter.)

    The reason LengthMetaclass.__len__ doesn’t affect instances of A is that attribute resolution in Python first checks the instance dict, then walks the class hierarchy [A, object], but it never consults the metaclasses. Whereas accessing A.__len__ first consults the instance A, then walks it’s class hierarchy, which consists of [LengthMetaclass, type].

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

Sidebar

Related Questions

Moving through the maze forward is pretty easy, but I can't seem to figure
When writing manual SQL its pretty easy to estimate the size and shape of
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
Pretty much every other editor that isn't a vi descendant (vim, cream, vi-emu) seems
Pretty sure this question counts as blasphemy to most web 2.0 proponents, but I
Pretty much what the title says really. We have some code that is .NET
Pretty simple scenario. I have a web service that receives a byte array that
The c#/XNA process for creating random numbers is pretty quick and easy, however, it
I just started using the Farseer engine and so far it's been pretty easy
pretty much what the title says. I am using an Ajax Drop Down as

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.