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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:04:56+00:00 2026-06-04T02:04:56+00:00

While integrating a Django app I have not used before, I found two different

  • 0

While integrating a Django app I have not used before, I found two different ways to define functions inside the class. The author seems to use them both distinctively and intentionally. The first one is the one that I myself use a lot:

class Dummy(object):

    def some_function(self, *args, **kwargs):
        # do something here
        # self is the class instance

The other one is the one I never use, mostly because I do not understand when and what to use it for:

class Dummy(object):

    @classmethod
    def some_function(cls, *args, **kwargs):
        # do something here
        # cls refers to what?

The classmethod decorator in the python documentation says:

A class method receives the class as the implicit first argument, just
like an instance method receives the instance.

So I guess cls refers to Dummy itself (the class, not the instance). I do not exactly understand why this exists, because I could always do this:

type(self).do_something_with_the_class

Is this just for the sake of clarity, or did I miss the most important part: spooky and fascinating things that couldn’t be done without it?

  • 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-04T02:04:57+00:00Added an answer on June 4, 2026 at 2:04 am

    Your guess is correct – you understand how classmethods work.

    The why is that these methods can be called both on an instance OR on the class (in both cases, the class object will be passed as the first argument):

    class Dummy(object):
    
        @classmethod
        def some_function(cls,*args,**kwargs):
            print cls
    
    #both of these will have exactly the same effect
    Dummy.some_function()
    Dummy().some_function()
    

    On the use of these on instances: There are at least two main uses for calling a classmethod on an instance:

    1. self.some_function() will call the version of some_function on the actual type of self, rather than the class in which that call happens to appear (and won’t need attention if the class is renamed); and
    2. In cases where some_function is necessary to implement some protocol, but is useful to call on the class object alone.

    The difference with staticmethod: There is another way of defining methods that don’t access instance data, called staticmethod. That creates a method which does not receive an implicit first argument at all; accordingly it won’t be passed any information about the instance or class on which it was called.

    In [6]: class Foo(object): some_static = staticmethod(lambda x: x+1)
    
    In [7]: Foo.some_static(1)
    Out[7]: 2
    
    In [8]: Foo().some_static(1)
    Out[8]: 2
    
    In [9]: class Bar(Foo): some_static = staticmethod(lambda x: x*2)
    
    In [10]: Bar.some_static(1)
    Out[10]: 2
    
    In [11]: Bar().some_static(1)
    Out[11]: 2
    

    The main use I’ve found for it is to adapt an existing function (which doesn’t expect to receive a self) to be a method on a class (or object).

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

Sidebar

Related Questions

While integrating two subsystems, we were forced to use multiple SessionFactory instances, which lead
A while back, online apps used to say, do not click submit more than
Background I am considering various architecture options. While I have used SQL over the
i am integrating CHartBoost, and while integrating i am getting few issue. I have
My company is currently using Sage MAS as their ERP system. While integrating our
While I do a program, sometimes I've got this doubt. I have been using
While Zope 2.13 have native wsgi support, I haven't been able to find any
I am new in this field.i am integrating LinkedIn in my native application.While i
While integrating backgroundrb-rails3 I get the following error, ~/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at
I have a web application that is integrating with Twitter's OAuth API. I registered

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.