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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:06:23+00:00 2026-05-14T15:06:23+00:00

Need to create a class that will do all things as the merge function.

  • 0

Need to create a class that will do all things as the “merge” function. In class i will change, process and add new arguments.

def merge(*arg, **kwarg): # get decorator args & kwargs
    def func(f):
        def tmp(*args, **kwargs): # get function args & kwargs    
            kwargs.update(kwarg) # merge two dictionaries
            return f(*args, **kwargs) # return merged data
        return tmp
    return func

Usage:

@other_decorator # return *args and **kwarg
@merge(list=['one','two','three']) # need to merge with @other_decorator
def test(*a, **k): # get merged args and kwargs
    print 'args:', a
    print 'kwargs:', k
  • 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-14T15:06:23+00:00Added an answer on May 14, 2026 at 3:06 pm

    I’m not sure I quite get what you’re asking. Your implementation works fine, and you won’t get around having two levels of indirection if you want to create a parametrized decorator of any kind.

    To make merge a class you could do this

    class Merge(object):
        def __init__(self, **extra_kws):
            self.extra_kws = extra_kws
        def __call__(self, function):
            def _wrapper(*args, **kws):
                kws.update(self.extra_kws)
                return function(*args, **kws)
            return _wrapper
    

    Then you can do this:

    @Merge(foo='bar')
    def test(*args, **kws):
        print *args
        print **kws
    

    But you said you want to add change and process new arguments. So presumably you want the decorator itself to be live so you can do:

    test.extra_kws['sun'] = 'dock'
    

    After the decorator has been applied. In that case you probably don’t want merge to be a class, but you want it to generate a class, so that test is replaced by the modifiable instance:

    def merge(**extra_kws):
        class _Merge(object):
            def __init__(self, function):
                self.extra_kws = extra_kws
                self.function = function
            def __call__(self, *args, **kws):
                kws.update(self.extra_kws)
                return self.function(*args, **kws)
        return _Merge
    
    @merge(foo='bar')
    def test(*args, **kws):
        print 'args:', args
        print 'kws:', kws
    
    test(sun='dock')
    test.extra_kws['trog'] = 'cube'
    test(sun='dock')
    

    This then allows you to change the keywords on a particular decorated function later.

    You could also do the same thing with function arguments without classes:

    def merge(**extra_kws):
        def _decorator(function):
            def _wrapper(*args, **kws):
                kws.update(_wrapper.extra_kws)
                return function(*args, **kws)
            _wrapper.extra_kws = extra_kws
            return _wrapper
        return _decorator
    
    @merge(foo='bar')
    def test(*args, **kws):
        print 'kws:', kws
    
    test(sun='dock')
    test.extra_kws['trog'] = 'cube'
    test(sun='dock')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The incoming Request has a ClientCertificates collection that you can… May 15, 2026 at 4:02 am
  • Editorial Team
    Editorial Team added an answer When the mapping mode is mm_Text (which it usually is),… May 15, 2026 at 4:02 am
  • Editorial Team
    Editorial Team added an answer You have (at least) two options. You could either include… May 15, 2026 at 4:02 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.