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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:40:05+00:00 2026-06-12T19:40:05+00:00

Python allows to call a static method not only from a class, but also

  • 0

Python allows to call a static method not only from a class, but also from an instance:

class X:
  @staticmethod
  def f():
    print('f')

x = X()
X.f() 
x.f() # same as above

This may be convenient when we only have an instance to work with; after all, who wants to write x.__class__.f() instead of x.f().

But I found that many readers of the code (including myself) tend to interpret x.f() as if it’s an instance method. That is, they assume that whatever is done either uses or changes x. In some cases, this even resulted in bugs (where the developer incorrectly interpreted the semantics of f).

So I was thinking to adopt a convention where all static methods are called only using the class object. Are there any static analysis tools that would warn me if this convention is violated?

  • 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-12T19:40:06+00:00Added an answer on June 12, 2026 at 7:40 pm

    I don’t think this amount of static checking is pythonic, but…

    class enforced_staticmethod(staticmethod):
         def __get__(self, instance, cls):
             if instance is not None:
                 raise Exception('Do not call with an instance.')
             return super(enforced_staticmethod, self).__get__(self)
    
    
    class C:
        @enforced_staticmethod
        def hai(x):
            return x + 1
    

    And you can test:

    >>> C.hai(10)
    11
    >>> C().hai(10)
    Traceback (most recent call last):
      File "<pyshell#52>", line 1, in <module>
        C().hai(10)
      File "<pyshell#48>", line 4, in __get__
        raise Exception('Do not call with an instance.')
    Exception: Do not call with an instance.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The yield statement in python allows simple iteration from a procedure, and it also
From what I understand, Python lambda's only allow expressions, not statements. I have a
Maybe I'm spoiled by Python, but does Octave allows one to assign the values
If statements in Python allow you to do something like: if not x: print
In Python, you can assign an arbitrary attribute from outside the defining class :
I've got a class that is essentially mutable, but allows for some persistent-like operations.
I know that Python, unlike Java, supports inheritance. But does a user class can
In Python, the following statements do not work: f = open("ftmp", "rw") print >>
Why doesn't Python allow modules to have a __call__ method? (Beyond the obvious that
Python allows aliasing of imports, through ...as <ALIAS> clauses in the import statement, like

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.