Does it ever make sense to write 2 classes with identical names in the same python program?

Question:

Are there any scenarios where it would make sense to write two python classes in the same program with the same name using different parameters? I was thinking through this program, but my 2nd test class will just overwrite the first. Is this always the case?

class test:
    def __init__(self):
        print("first class")
    def oneplus(self, x):
        print(x + 1)

class test:
    def __init__(self):
        print("second class")
    def twoplus(self, x):
        print(x + 2)

t = test()
t.twoplus(1)  

will just result in using the 2nd instance:

second class
3
Asked By: barker

||

Answers:

yes, if you define a class with the same name as an already existing class, it will override the definition.

BUT existing instances of the first class will still behave as usual.

small example:

class test:
    def __init__(self):
        print("first class")
    def oneplus(self, x):
        print(x+1)

t1 = test()

class test:
    def __init__(self):
        print("second class")
    def twoplus(self, x):
        print(x+2)

t2=test()
t1.oneplus(1)
t2.twoplus(1)

Output:

first class
second class
2
3

If you never use the first class, IDEs like PyCharm will even warn you about this:
enter image description here

Answered By: Adam.Er8

As mentioned above, the second assignment of test will override the first assignment. Basically, when a class is assigned in Python it is stored in memory with a type, value, and reference count. In this case the first assignment of “Test” creates a python object (Test1) with a reference count of 1 and the second assignment of “Test” creates another python object (Test2) with a reference count of 1, but “Test” is no longer referencing the original python object (Test1) so its reference count decreases to 0.

Example of names in Python.

Great resource for python objects: https://realpython.com/pointers-in-python/

Answered By: jdpy19

Unless you plan on making the system recognize the second definition only, then making two variables with the same name will not make a difference.

Answered By: HamTheBurger

A program may consist of more than one module. In that case, it can make a lot of sense to have a class with the same name. For example if you have a feature that works differently in Windows vs Linux, you can have a module implementing the windows-specific stuff, and a module implementing the Linux-specific stuff. Both would have classes with identical names. In your code, you could do

if os.name == nt:
    from windows_support import Feature
else:
    from linux_support import Feature

This could be done for any external influence, not just the platform.

If you have only a small number of differences, usually ones that are a subset of the module in question, you can have two classes with identical names in different branches of an if statement. For example:

if os.name == 'nt':
    class Feature:
        ...
else:
    class Feature:
        ...

The statements def, class, and most forms of import are assignments. It makes just as much sense to assign a different value to a name under different conditions as it does a different class or function. After all, classes are just objects in python.

That being said, all the examples above work best if both options implement the same public interface.

An example of different classes that unconditionally have the same name in the same namespace that serves a real life purpose is forward declaration for type hints. Until PEP 563 introduced delayed evaluation of annotations in Python 3.7, it was impossible to use a class name in an annotation until it was assigned. By extension, that meant that methods of a class could not accept or return instances of the class they were defined in, and circular references between classes were not supported without a workaround. That workaround was to define an empty definition of a class with the same name, before the classes that would be using it in type hints. This would actually keep a reference to the forward-declared throwaway class around, which was one of the motivations for PEP 563.

Answered By: Mad Physicist

You can add 1 more method in Test class,

class test:
   def __init__(self):
      print("first class")
   def oneplus(self, x):
      print(x + 1)

   def twoplus(self, x):
      print(x + 2)

t = test()
t.oneplus(1)
t.twoplus(1)  
Answered By: Fahrizal
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.