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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:55:34+00:00 2026-06-05T07:55:34+00:00

How can I tell if a class instance method and function are the same?

  • 0

How can I tell if a class instance method and function are the same?

I have a simple class that uses a decorator:

@decorate_class
class Hello:

    @decorate_func
    def world(self):
        return

And these are my decorators:

# A global variable
global_func = None

def decorate_func(func):
    global_func = func
    return func

def decorate_class(clazz):
    print clazz.__dict__["world"] == global_func
    return clazz

The above returns False, possible because type(func) in decorate_func is function, but in decorate_class it is instancemethod. But printing both of them gives me:

<function world at 0x7f490e59ce60>

As in, the same memory address. How do I compare them to know that they are the same function? Is comparing by the memory address safe (and correct)?

  • 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-05T07:55:35+00:00Added an answer on June 5, 2026 at 7:55 am

    Your problem here is actually your use of global_func.

    If you change decorate_class to:

    def decorate_class(clazz):
        print global_func, clazz.__dict__["world"] == global_func
        return clazz
    

    You’ll see that global_func is None.

    To fix this, explicitly declare global_func as global in decorate_func:

    def decorate_func(func):
        global global_func
        global_func = func
        return func
    

    And everything will work.

    This is (basically) because Python assumes that, if you assign to a variable in a function, that variable is assumed to be local to that function, unless it is explicitly declared to be global.

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

Sidebar

Related Questions

I have developed a Jersey Resource class. Can someone please tell me how can
I have been trying to create a decorator that can be used with both
In my main Actionscript file i have a instance of a body class that
As far as I can tell, Scala has definitions for the Enumeration Value class
I am confused about class size of these two classes. who can tell me
Can Any body tell why window class library can't be added in the silverlight
Can someone tell me why I cannot write a class as follows <?php class
Can anyone tell me what the scope of the static variable in the class
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
can somebody tell me why my code is not working? class Connection { public

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.