Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populating instances of class automatically #79520

Closed
Daugeras mannequin opened this issue Nov 28, 2018 · 3 comments
Closed

Populating instances of class automatically #79520

Daugeras mannequin opened this issue Nov 28, 2018 · 3 comments
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error

Comments

@Daugeras
Copy link
Mannequin

Daugeras mannequin commented Nov 28, 2018

BPO 35339
Nosy @Wooble

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2018-11-29.06:41:01.471>
created_at = <Date 2018-11-28.17:05:42.662>
labels = ['invalid', 'type-bug', '3.7']
title = 'Populating instances of class automatically'
updated_at = <Date 2018-11-29.06:41:01.470>
user = 'https://bugs.python.org/Daugeras'

bugs.python.org fields:

activity = <Date 2018-11-29.06:41:01.470>
actor = 'steven.daprano'
assignee = 'none'
closed = True
closed_date = <Date 2018-11-29.06:41:01.471>
closer = 'steven.daprano'
components = []
creation = <Date 2018-11-28.17:05:42.662>
creator = 'Daugeras'
dependencies = []
files = []
hgrepos = []
issue_num = 35339
keywords = []
message_count = 3.0
messages = ['330613', '330614', '330630']
nosy_count = 2.0
nosy_names = ['geoffreyspear', 'Daugeras']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue35339'
versions = ['Python 3.7']

@Daugeras
Copy link
Mannequin Author

Daugeras mannequin commented Nov 28, 2018

Hello, I am not sure it is a bug, but it is a very strange behavior of Python, which I do not understand.

I created a class (mainly a structure) and instantiated an object of this class in a first function (Object 1). The function populates Object 1.
In another function, I instantiate another object of the same class (Object 2) , and Object 2 is automatically populated with the data of Object 1. Note than Object 1 and Object 2 have different memory adresses.

This behavior is very strange. Is it normal for Python or is it a Bug ?

@Daugeras Daugeras mannequin added 3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error labels Nov 28, 2018
@Wooble
Copy link
Mannequin

Wooble mannequin commented Nov 28, 2018

It's impossible to guess exactly what you did from your vague description instead of actual code, but it may be covered by this FAQ question: https://docs.python.org/3/faq/programming.html#why-are-default-values-shared-between-objects

(mutable objects as class rather than instance attributes will display similar behavior. This isn't a bug.)

@Daugeras
Copy link
Mannequin Author

Daugeras mannequin commented Nov 28, 2018

Hi Geoffrey,

Indeed you where right, this is not a bug.
The link you provided did solve my problem.
I declared classes with attributes as strings, lists, etc... and indeed it was the reason for the issue.
I changed it to instantiate the attributes in the __init__ method of the class, and it solved the problems.
Thanks again !!

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.7 (EOL) end of life type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant