Multiple instances of a class being overwritten at the same time? (Python)

2024/10/2 14:24:07

Here's a very simple code I made to demonstrate the problem I'm encountering. What's happening here is that I'm creating two different instances of the same class but changing an attribute of one will change the corresponding attribute of the other instance. I'm not sure why this is. Is this normal in Python or am I encountering something being totally messed up?

class exampleClass(object):attribute1=0attribute2="string"x=exampleClass
x.attribute1=20
x.attribute2="Foo"y=exampleClass
y.attribute1=60
y.attribute2="Bar"print("X attributes: \n")
print(x.attribute1)
print(x.attribute2)print("Y attributes: \n")
print(y.attribute1)
print(y.attribute2)

Here is what the program looks like coming out of my console:

>>> 
X attributes: 60
Bar
Y attributes: 60
Bar
>>> 

I think it should be saying:

X attributes:20
Foo
Y attributes:60
Bar

What am I doing wrong?

Answer

You're creating class attributes, when you want instance attributes. Use this:

class exampleClass(object):def __init__(self):self.attribute1 = 0self.attribute2 = "string"

Also, you aren't creating any instances of exampleClass, you need this:

x = exampleClass()
y = exampleClass()

Your code is simply using new names for the class, and changing its attributes.

https://en.xdnf.cn/q/70839.html

Related Q&A

how to store binary file recieved by Flask into postgres

I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route(/upload, methods=[POST]) def upload_file():def allowed_file(f):return Truefile …

How can I kill a single shot QtCore.QTimer in PyQt4?

So, in my application, I create a single QtCore.QTimer object and then call the singleShot method on it to evoke a function after say 60 secs. Now, at any given point in time, if I would need to call t…

How to convert list of lists to a set in python so I can compare to other sets?

I have a list users_with_invites_ids_list, formed by loop where I append values to the list, in python that looks like this:...[ObjectId(55119e14bf2e4e010d8b48f2)], [ObjectId(54624128bf2e4e5e558b5a52)]…

How can I create an ODBC connection to SAS?

Im writing a program that needs to access SAS data. Ive downloaded the ODBC drivers for SAS and installed them, but I need to be able to create ODBC connections on the fly, programmatically. The foll…

How to extract links from a page using Beautiful soup

I have a HTML Page with multiple divs like:<div class="post-info-wrap"><h2 class="post-title"><a href="https://www.example.com/blog/111/this-is-1st-post/" t…

Verifying the integrity of PyPI Python packages

Recently there came some news about some Malicious Libraries that were uploaded into Python Package Index (PyPI), see:Malicious libraries on PyPI Malicious modules found into official Python repository…

How to get results from custom loss function in Keras?

I want to implement a custom loss function in Python and It should work like this pseudocode:aux = | Real - Prediction | / Prediction errors = [] if aux <= 0.1:errors.append(0) elif aux > 0.1 &am…

How to tell whether a file is executable on Windows in Python?

Im writing grepath utility that finds executables in %PATH% that match a pattern. I need to define whether given filename in the path is executable (emphasis is on command line scripts).Based on "…

Issue with python/pytz Converting from local timezone to UTC then back

I have a requirement to convert a date from a local time stamp to UTC then back to the local time stamp.Strangely, when converting back to the local from UTC python decides it is PDT instead of the or…

Regex to replace %variables%

Ive been yanking clumps of hair out for 30 minutes doing this one...I have a dictionary, like so:{search: replace,foo: bar}And a string like this:Foo bar %foo% % search %.Id like to replace each var…