# Modify a function through a decorator

**URL:** <https://discuss.python.org/t/modify-a-function-through-a-decorator/68881>\
**Category:** Python Help\
**Created:** [October 23, 2024, 10:08am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881 "2024-10-23T10:08:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SpongeB0B](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/spongeb0b/32/13257_2.png) [@SpongeB0B](https://discuss.python.org/u/SpongeB0B)\
**Post date:** [October 23, 2024, 10:08am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/1 "2024-10-23T10:08:58Z")

</div>

Hi,

```python
def addon(func):
	func. __dict__ ['test'] = 2

	return func

@addon
def initial(test=1):
	print("I'm initial with test " + str(test))

initial()

```

give me as output

> I’m initial with test 1

I was expecting " I’m initial with test **2**"

How can I change the kwarg of the decorated function ? (with the less lines possibles, so meaning without a wrapper ?!

Thanks.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 23, 2024, 10:11am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/2 "2024-10-23T10:11:09Z")

</div>

A function’s default arguments are stored in the ` __defaults__ ` attribute (or ` __kwdefaults__ ` for kwonly arguments). Try printing out `initial. __defaults__ `.

---

<div class="post-metadata">

**Author:** ![SpongeB0B](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/spongeb0b/32/13257_2.png) [@SpongeB0B](https://discuss.python.org/u/SpongeB0B)\
**Post date:** [October 23, 2024, 10:19am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/3 "2024-10-23T10:19:05Z")

</div>

Thank you very much ` __defaults__ ` give me : `(1,)`

but I would like access/change them by name rather than by index… is it possible ?

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 23, 2024, 10:21am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/4 "2024-10-23T10:21:51Z")

</div>

> [@SpongeB0B](#):
>
> but I would like access/change them by name rather than by index… is it possible ?

You can make it a keyword-only parameter, and then it’ll be in ` __kwdefaults__ ` by name instead. Otherwise, you’ll have to match it up with what you find from the variable names.

---

<div class="post-metadata">

**Author:** ![SpongeB0B](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/spongeb0b/32/13257_2.png) [@SpongeB0B](https://discuss.python.org/u/SpongeB0B)\
**Post date:** [October 23, 2024, 11:14am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/5 "2024-10-23T11:14:54Z")

</div>

indeed

```python
def addon(func):
	print(func. __kwdefaults__ )
	func. __dict__ ['test'] = 2
	return func

@addon
def initial(*,test=1):
	print("I'm initial with test " + str(test))

initial()

```

> {‘test’: 1}  
> I’m initial with test 1

So I can’t still change the `test` kwarg with the addon decorator… 😕

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [October 23, 2024, 11:22am UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/6 "2024-10-23T11:22:57Z")

</div>

> [@SpongeB0B](#):
>
> ```python
> print(func. __kwdefaults__ )
> func. __dict__ ['test'] = 2
> 
> ```

Do you notice what’s going on here? Setting the function’s dictionary is NOT changing its local variables.

---

<div class="post-metadata">

**Author:** ![onePythonUser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/onepythonuser/32/19158_2.png) [@onePythonUser](https://discuss.python.org/u/onePythonUser)\
**Post date:** [October 23, 2024, 12:50pm UTC](https://discuss.python.org/t/modify-a-function-through-a-decorator/68881/7 "2024-10-23T12:50:14Z")

</div>

Hello,

can you use a class as the decorator:

```python
class addon:

    def __init__ (self, func):                                     
        self.func = func

    def __call__ (self, *args):   
        return self.func(2) # Change value here as the argument

@addon
def spam(test = 1):               
                                
    print("I'm initial with test: ", test)

spam()

```
