'Why does Pytest create a new class instance for each test method?

I was reading the Pytest documentation when I noticed a section titled "Grouping multiple tests in a class". There's a paragraph below with a caveat that each test method gets a unique copy of the class instance.Pytest provided a counter-example for why sharing the class instance would be a bad idea.

I don't quite understand this example. Could you help me understand the point being made with this example?

"""
Something to be aware of when grouping tests inside classes is that each test has a 
unique instance of the class. Having each test share the same class instance would be 
very detrimental to test isolation and would promote poor test practices. 
This is outlined below:
"""


# content of test_class_demo.py
class TestClassDemoInstance:
    def test_one(self):
        assert 0

    def test_two(self):
        assert 0
$ pytest -k TestClassDemoInstance -q
FF                                                                   [100%]
================================= FAILURES =================================
______________________ TestClassDemoInstance.test_one ______________________

self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>

    def test_one(self):
>       assert 0
E       assert 0

test_class_demo.py:3: AssertionError
______________________ TestClassDemoInstance.test_two ______________________

self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>

    def test_two(self):
>       assert 0
E       assert 0

test_class_demo.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_class_demo.py::TestClassDemoInstance::test_one - assert 0
FAILED test_class_demo.py::TestClassDemoInstance::test_two - assert 0
2 failed in 0.12s


Solution 1:[1]

The example above states that:

Something to be aware of when grouping tests inside classes is that each test has a 
unique instance of the class.
______________________ TestClassDemoInstance.test_one ______________________

self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>

    def test_one(self):
>       assert 0
E       assert 0

test_class_demo.py:3: AssertionError
______________________ TestClassDemoInstance.test_two ______________________

self = <test_class_demo.TestClassDemoInstance object at 0xdeadbeef>

However, both TestClassDemoInstance in test_one & test_two point to the same object address. This example will be clearer if the addresses for the two objects are different to signify object uniqueness.

Solution 2:[2]

I wonder why nobody ran the code and checked.

After running below code -

class TestClassDemoInstance:
def test_one(self):
    assert 0

def test_two(self):
    assert 0

pytest --verbosity=1 code.py

I got two different address unlike mentioned in the documentation.

self = <random_code.TestClassDemoInstance object at 0x7ff8009c1190>
self = <random_code.TestClassDemoInstance object at 0x7ff8009c1890>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 qbit
Solution 2 user5319825