# New, copy, clone or duplicate?

**URL:** https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31
**Category:** Uncategorized
**Created:** [May 19, 2016, 7:29pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31 "2016-05-19T19:29:21Z")
**Posts on this page:** 12
**Page:** 1

<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 19, 2016, 7:29pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/1 "2016-05-19T19:29:21Z")

</div>

We often need to declare a local `Signal` with the same properties as a port. I used `localsignal = Signal(intbv(0)[len(portsignal):])` but this only works for `intbv` types. Chris taught me: `localsignal = Signal(portsignal._val)` which is a bit shorter. I don’t remember whether this also works for `bool()`.  
Now to write _generic_ code I deemed it more interesting to have a method to create this _new_ signal. So I added a `new()` method to the Signal class, but then I found out that Python has a hidden ` __new__ ()` so I changed the name to `copy()`. Of course this now collides somewhat with Python’s copy() and deepcopy(). I don’t like the name `clone` although this may be the best match. How about using `duplicate()` or shortened: `dup()`?  
The rationale for having this functionality is that new types like `Array` (MEP-112 under development) and `StructType` (No MEP yet) would benefit from such an easier method to create a _new_ object.

Regards,

Josy

---

<div class="post-metadata">

### Author: ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)
#### Post date: [May 22, 2016, 11:18am UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/2 "2016-05-22T11:18:28Z")

</div>

Other Python objects have a `copy` method, that would probably be the correct approach. Example both `list` and `dict` objects have a copy method.

---

<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 23, 2016, 8:06am UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/3 "2016-05-23T08:06:49Z")

</div>

but in Python `copy()` is shallow; to get a _new_ Signal (or Array or …) it would have to be `deepcopy()`. I would like to stay away from the Python semantics and avoid any flak 😟  
Also `copy()` and `deepcopy` should (?) be implemented as hooks; ` __copy__ ()` and ` __deepcopy__ ()`, and then be used as:

```python
import copy

. . . 

a = copy.deepcopy(b)

```

Which is very well understood by experienced Python user, but takes a bit of study for the VHDL, Verilog user base.  
Where as a `dup()` would be a method:

```python

a = b.dup()

```

---

<div class="post-metadata">

### Author: ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)
#### Post date: [May 23, 2016, 2:59pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/4 "2016-05-23T14:59:07Z")

</div>

I think we might differ in opinion on this one. Following the “Python way” has served us well, i.e. we leverage their language design experience. I would propose the ` __copy__ ` and ` __deepcopy__ ` but also add a shortcut with a `copy` method.

```python
x = Signal(sigtype)
y = x.copy()

a = [sigtype for _ in range(16)]
b = copy.deepcopy(a)

```

---

<div class="post-metadata">

### Author: ![smldis](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@smldis](https://discourse.myhdl.org/u/smldis)
#### Post date: [May 23, 2016, 3:14pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/5 "2016-05-23T15:14:10Z")

</div>

Hi, looking at \_Signal.py I would like to propose `localsignal = Signal(portsignal)`.  
We usually pass an instance of something to the Signal constructor and that is then deepcopied.

---

<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 23, 2016, 3:16pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/6 "2016-05-23T15:16:23Z")

</div>

I see your point, and agree, to some extent.  
The ` __copy__ ()` hook would not be of much use, as we can’t do much with a shallow copy.  
Implementing ` __deepcopy__ ()` is then what we need, calling the short-cut _copy()_ could create some confusion.  
Am I right in reading your second example as: Python’s copy() function will iterate through the list and call on the `_deepcopy__()` from `sigtype`?

---

<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 23, 2016, 3:18pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/7 "2016-05-23T15:18:49Z")

</div>

This will not work for all (present and future) types, e.g. if the _portsignal_ is an `Array` we can’t expect the Signal class to handle it …

---

<div class="post-metadata">

### Author: ![smldis](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@smldis](https://discourse.myhdl.org/u/smldis)
#### Post date: [May 23, 2016, 3:30pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/8 "2016-05-23T15:30:00Z")

</div>

I see… `Signal(Array([1,0,1]))` will not work? No .next for Arrays?

---

<div class="post-metadata">

### Author: ![cfelton](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.myhdl.org/cfelton/32/3_2.png) [@cfelton](https://discourse.myhdl.org/u/cfelton)
#### Post date: [May 23, 2016, 4:24pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/9 "2016-05-23T16:24:41Z")

</div>

@smldis you can do this with:

```python
localsignal = Signal(portsignal.val)

```

I think that is reasonable for shallow init.

---

<div class="post-metadata">

### Author: ![smldis](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@smldis](https://discourse.myhdl.org/u/smldis)
#### Post date: [May 23, 2016, 5:07pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/10 "2016-05-23T17:07:44Z")

</div>

that’s better! I was just trying to point out that syntax like `intbv(intbv(0)[8:])` is something familiar in myhdl codebase. In general this could be extended to every myhdl “types” and even Signals.

---

<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 23, 2016, 5:46pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/11 "2016-05-23T17:46:02Z")

</div>

At the moment I have Array(shape, type) loosely modelled after the numpy ndarray. It has a.next property,of course! And a few extra goodies …

---

<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 23, 2016, 7:32pm UTC](https://discourse.myhdl.org/t/new-copy-clone-or-duplicate/31/12 "2016-05-23T19:32:48Z")

</div>

Chris,  
I just found out that \_intbv.py has (almost) identical code for ` __copy__ ` and ` __deepcopy__ `. If we adopt this further `copy()` would be acceptable for the _shortcut_ method. (and would be almost identical code too). This will simplify my code for _Array()_ (and _StructType()_)

Regards,

Josy
