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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:56:13+00:00 2026-06-05T06:56:13+00:00

Possible Duplicate: What is the difference between @staticmethod and @classmethod in Python? I have

  • 0

Possible Duplicate:
What is the difference between @staticmethod and @classmethod in Python?

I have a few questions about staticmethods in classes. I will start by giving an example.

Example one:

class Static:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.age = randint(0, 50)
    def printName(self):
        return self.first + self.last
    @staticmethod
    def printInfo():
        return "Hello %s, your age is %s" % (self.first + self.last, self.age)

x = Static("Ephexeve", "M").printInfo()

Outputs:

Traceback (most recent call last):
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 90, in <module>
    x = Static("Ephexeve", "M").printInfo()
  File "/home/ephexeve/Workspace/Tests/classestest.py", line 88, in printInfo
    return "Hello %s, your age is %s" % (self.first + self.last, self.age)
NameError: global name 'self' is not defined

Example two:

class Static:
    def __init__(self, first, last):
        self.first = first
        self.last = last
        self.age = randint(0, 50)
    def printName(self):
        return self.first + self.last
    @staticmethod
    def printInfo(first, last, age = randint(0, 50)):
        print "Hello %s, your age is %s" % (first + last, age)
        return

x = Static("Ephexeve", "M")
x.printInfo("Ephexeve", " M") # Looks the same, but the function is different.

Outputs

Hello Ephexeve M, your age is 18

I see I cannot call any self.attribute in a staticmethods, I am just not really sure when and why to use it. In my mind, if you create a class with a few attributed, perhaps you want to use them later on, and not have a staticmethod where all attributes are not callable.
Anyone can explain me this? Python is my first programming langunge, so if this is the same in Java for example, I don’t know.

  • 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-05T06:56:14+00:00Added an answer on June 5, 2026 at 6:56 am

    What are you trying to achieve with that staticmethod? How are you expecting it to solve whatever your problem is if you don’t know what it does?

    Or are you just playing around to see what staticmethod does? In that case, it would probably be more productive to read the docs to be told what it does, rather than randomly applying it and trying to guess from the behaviour what it does.

    In any case, applying @staticmethod to a function definition in a class makes a “static method”. “Static” is unfortunately one of the most confusingly overloaded terms in programming; here it means that the method does not depend on or alter the object’s state. If I have a static method foo defined in a class Bar, then calling bar.foo(...) (where bar is some instance of the class Bar) will do exactly the same thing regardless of what bar‘s attributes contain. Indeed I can call it directly from the class as Bar.foo(...) when I don’t even have an instance!

    This is achieved by simply not passing the instance into the static method, thus static methods have no self parameter.

    Static methods are rarely necessary, but occasionally convenient. They are really the same as simple functions defined outside the class, but putting them in the class marks them as “associated” with the class. You’d generally use them for calculating or doing things that are closely related to the class, but aren’t actually operations on some particular object.

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

Sidebar

Related Questions

Possible Duplicate: What is the difference between @staticmethod and @classmethod in Python? I am
Possible Duplicate: Difference between object and instance I have couple of questions: Every instance
Possible Duplicate: difference between string object and string literal Let's say I have two
Possible Duplicate: Difference between try-catch syntax for function Few days ago I was reading
Possible Duplicate: Calculating the Difference Between Two Java Date Instances hi, I have two
Possible Duplicate: Difference between (function(){})(); and function(){}(); I have seen it a lot by
Possible Duplicate: Difference between static class and singleton pattern? I'm thinking about the choice
Possible Duplicate: C - Difference between char var[] and char *var? I have written
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: difference between abstraction and encapsulation? What is the difference between abstraction and

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.