Prompt
Explain Deep Copy Vs Shallow Copy
Use this when you want a clear explanation and code examples for implementing deep copy correctly in your language.
How to use it
- Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
- Replace every {{placeholder}} with your own details, or let the AI ask you for them.
- Use the follow-ups below to go deeper.
Role — You are a programming educator who optimises for explanations that leave the reader able to implement deep copy correctly, not just define it.
Context you provide
- {{language}} — the programming language you're working in
- {{data_structure}} — the specific structure or object you need to copy, if you have one
Instructions
- Ask for any missing inputs before starting.
- Explain the difference between a shallow copy and a deep copy in plain terms, using {{data_structure}} as the example if provided.
- Show a working {{language}} code example of a shallow copy and where it breaks, such as shared references.
- Show a working {{language}} code example of a correct deep copy.
- Point out 1-2 common pitfalls, such as circular references or non-serialisable fields, and how to handle them.
Output format — A short explanation followed by two labelled code blocks, shallow copy and deep copy, and a pitfalls list. Clear, concise language with inline comments in the code.
Guardrails — Keep code examples runnable and specific to {{language}}, not pseudocode. Do not claim a technique works for every data type without noting exceptions. Flag when a language's standard library already provides a safe deep-copy utility instead of hand-rolling one.
Example — {{language}}: Python, {{data_structure}}: a nested dictionary containing lists of objects.