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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T01:12:10+00:00 2026-06-09T01:12:10+00:00

How do I pass a class field to a decorator on a class method

  • 0

How do I pass a class field to a decorator on a class method as an argument? What I want to do is something like:

class Client(object):
    def __init__(self, url):
        self.url = url

    @check_authorization("some_attr", self.url)
    def get(self):
        do_work()

It complains that self does not exist for passing self.url to the decorator. Is there a way around this?

  • 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-09T01:12:11+00:00Added an answer on June 9, 2026 at 1:12 am

    Yes. Instead of passing in the instance attribute at class definition time, check it at runtime:

    def check_authorization(f):
        def wrapper(*args):
            print args[0].url
            return f(*args)
        return wrapper
    
    class Client(object):
        def __init__(self, url):
            self.url = url
    
        @check_authorization
        def get(self):
            print 'get'
    
    >>> Client('http://www.google.com').get()
    http://www.google.com
    get
    

    The decorator intercepts the method arguments; the first argument is the instance, so it reads the attribute off of that. You can pass in the attribute name as a string to the decorator and use getattr if you don’t want to hardcode the attribute name:

    def check_authorization(attribute):
        def _check_authorization(f):
            def wrapper(self, *args):
                print getattr(self, attribute)
                return f(self, *args)
            return wrapper
        return _check_authorization
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Source from copy import deepcopy class Field(object): def __init__(self): self.errors = [] class BaseForm(object):
Suppose code like this: class Base: def start(self): pass def stop(self) pass class A(Base):
I have class: @custom_decorator class Creature(objects): def __init__(self): pass def rise_hands(self, *args, **kwargs): print
I want pass a class-instace to a method of an other and be sure
Is it possible to pass TargetActivity.class to another activity and do something like: //
How to pass a class and a method name as strings and invoke that
I would like to pass custom options into my field. Is there a way
Given the following models: class Graph(models.Model): owner = models.ForeignKey(User) def __unicode__(self): return u'%d' %
If a class need multiple field information during object creation and also it is
When developing a query to pass to a class or some other field filled

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.