# How to write copying & pasting code macro like \`include\`?

**URL:** <https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565>\
**Category:** New to Julia\
**Tags:** question, pycall\
**Created:** [November 4, 2020, 5:42am UTC](https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565 "2020-11-04T05:42:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paalon/32/5784_2.png) [@paalon](https://discourse.julialang.org/u/paalon)\
**Post date:** [November 4, 2020, 5:42am UTC](https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565/1 "2020-11-04T05:42:36Z")

</div>

I want to copy & paste some code from other files. I know there is `include` but I think it is to include them to module’s global scope. I uses PyCall, and to implement Pythonic class, I need to write definitions of classes in module’s ` __init__ ` function.

```julia
module A
using PyCall
const Dog = PyNULL()
function __init__ ()
    @pydef mutable struct _Dog
        function __init__ (self, weight = 10)
            self.weight = weight
        end
        function greet(self)
            println("Bark Bark!")
        end
    end
    copy!(Dog, _Dog)
end
end

```

I want to move `@pydef ... end` to another file. It seems good to write such macro, but I don’t know what to do.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 4, 2020, 10:55am UTC](https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565/2 "2020-11-04T10:55:14Z")

</div>

I think that `structs` can only be defined in the module global scope, so I am not sure if you are getting what you are expecting there.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 5, 2020, 10:53am UTC](https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565/3 "2020-11-05T10:53:38Z")

</div>

`eval` in the module’s namespace should work if you construct the expression above as an AST. See the metaprogramming chapter of the manual.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [November 5, 2020, 11:18am UTC](https://discourse.julialang.org/t/how-to-write-copying-pasting-code-macro-like-include/49565/4 "2020-11-05T11:18:07Z")

</div>

> [@lmiq](#):
>
> I think that `structs` can only be defined in the module global scope, so I am not sure if you are getting what you are expecting there.

That’s true for actual struct definitions, but the `@pydef` macro is not actually creating a Julia struct, but creating a `PyObject` that’s the Python class `_Dog` and assigning it to the variable `_Dog`.

Regarding the OP’s original question: If you want to put the class definition in another file, I would recommend putting the definition inside a function, then including that file containing this function and just calling that function inside ` __init__ `. E.g., you could create a file `dog.jl` in the same directory as your other file containing the following:

```julia
function create_dog_class()
    @pydef mutable struct _Dog
        function __init__ (self, weight = 10)
            self.weight = weight
        end
        function greet(self)
            println("Bark Bark!")
        end
    end
    return _Dog
end

```

Then you can do

```julia
module A
using PyCall
const Dog = PyNULL()

include("dog.jl")

function __init__ ()
    copy!(Dog, create_dog_class())
end
end

```

in your other file.
