# Unexpected Output with Default parameters

**URL:** <https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44>\
**Category:** Support\
**Created:** [May 27, 2016, 9:58am UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44 "2016-05-27T09:58:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sriramesh4](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@sriramesh4](https://discourse.myhdl.org/u/sriramesh4)\
**Post date:** [May 27, 2016, 9:58am UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/1 "2016-05-27T09:58:15Z")

</div>

This sample code [here](https://gist.github.com/srivatsan-ramesh/7766cf8f70b9a2b5b3c58cb348c4f6ca) produces an output waveform which is same for both the signals ‘sig’ of the instances ‘a’ and ‘b’.  
This happens only when I use the default parameter(In this example value for ‘sig’ is not passed when class object is created, instead the default value is used).  
The problem can be avoided by passing two different Signal variables(instead of using the default value) when the class object is created.  
Can someone tell me whether this is a problem with MyHDL or python or the way I coded?  
Maybe this is inevitable and I should not use default signal values?

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [May 27, 2016, 4:35pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/2 "2016-05-27T16:35:24Z")

</div>

I tried this:

```python
class Test:

    def __init__ (self, clock, sig=None):

        self.clock = clock
        self.sig = sig if sig is not None else Signal(bool(0))

    def process(self, i):

        @always(self.clock.posedge)
        def test():
            if i :
                self.sig.next = not self.sig
        return test

```

and then got the expected result.

---

<div class="post-metadata">

**Author:** ![sriramesh4](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@sriramesh4](https://discourse.myhdl.org/u/sriramesh4)\
**Post date:** [May 27, 2016, 5:05pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/3 "2016-05-27T17:05:33Z")

</div>

Ok, thanks. So, I have to change the way I code !

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [May 27, 2016, 5:06pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/4 "2016-05-27T17:06:13Z")

</div>

But do you know why? Think about it for a while …

---

<div class="post-metadata">

**Author:** ![sriramesh4](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@sriramesh4](https://discourse.myhdl.org/u/sriramesh4)\
**Post date:** [May 27, 2016, 5:23pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/5 "2016-05-27T17:23:18Z")

</div>

Yeah, now I understand. Function’s default arguments can only be immutable(whereas Signal(bool(0)) is mutable), because they are assigned during function definition and not during every function call.

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [May 27, 2016, 5:26pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/6 "2016-05-27T17:26:20Z")

</div>

Not the right answer. The simulation showed that both but a.sig and b.sig changed because …

---

<div class="post-metadata">

**Author:** ![sriramesh4](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@sriramesh4](https://discourse.myhdl.org/u/sriramesh4)\
**Post date:** [May 27, 2016, 5:43pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/7 "2016-05-27T17:43:45Z")

</div>

Since mutable type is used as default argument. In all the instances of the class, ‘sig’ will be referencing the same object. So when a.sig was changed b.sig also changed and vice versa.

---

<div class="post-metadata">

**Author:** ![josyb](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/josyb/32/5_2.png) [@josyb](https://discourse.myhdl.org/u/josyb)\
**Post date:** [May 27, 2016, 6:21pm UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/8 "2016-05-27T18:21:32Z")

</div>

That makes an ‘S’. 🙂

---

<div class="post-metadata">

**Author:** ![sriramesh4](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@sriramesh4](https://discourse.myhdl.org/u/sriramesh4)\
**Post date:** [May 28, 2016, 1:32am UTC](https://discourse.myhdl.org/t/unexpected-output-with-default-parameters/44/9 "2016-05-28T01:32:43Z")

</div>

Thank you 🙂
