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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:36:53+00:00 2026-05-27T02:36:53+00:00

I have a decorator like this: def region_required(view_func): def new_view(request, ctx = {}, *args,

  • 0

I have a decorator like this:

def region_required(view_func):
    def new_view(request, ctx = {}, *args, **kw):
        import pdb; pdb.set_trace()
        ctx['regions'] = Region.objects.all()
        return view_func(request, context=ctx, *args, **kw)
    return new_view

I uses it to decorate view functions.

Today I noticed something really strange, the ctx (context) argument sometimes have data that belongs to last HTTP request. Then I narrowed it down to this decorator and found that somehow ctx has value when it should be {} here.

So I set a break point, and goes one level up, then I found its caller does NOT pass anything to it at all.

How can an argument has value that is neither passed in by caller nor default value?

  • 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-05-27T02:36:54+00:00Added an answer on May 27, 2026 at 2:36 am

    You’re using a mutable argument as a default parameter. Every time it’s called without that argument, it will include the one you provided, including prior modifications. Use None as the default value instead, check for it, and if it is None, assign the empty dict in the body of the function.

    def region_required(view_func):
        def new_view(request, ctx=None, *args, **kw):
            if ctx is None:
                ctx = {}
            import pdb; pdb.set_trace()
            ctx['regions'] = Region.objects.all()
            return view_func(request, context=ctx, *args, **kw)
        return new_view
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a setup looking something like this: def foo_decorator(function): @wraps(function) def decorator(*args, **kwargs):
Assuming I have a decorator and a wrapped function like this: def squared(method): def
I have code that looks like this: Class Parent: def someMethod(self): return 42 Class
I typically write my scripts with a structure like s #!/usr/bin/python import stuff def
I have this decorator, used to decorate a django view when I do not
I have a class like this: class MyBase(object): x = 3 Documentation for property
I want a decorator that would add the decorated function to list, like this
from functools import wraps def foo_register(method_name=None): Does stuff. def decorator(method): if method_name is None:
Let's assume I have a class to be used as a decorator like such:
I would like to implement a decorator that provides per-request caching to any method,

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.