0

I have two different classes with same Subclass name and printing it out using type() gives me same output.

from enum import IntEnum class Car: class Manufacturer(IntEnum): BMW = 0 AUDI = 1 class Plane: class Manufacturer(IntEnum): AIRBUS = 0 BOEING = 1 print(type(Car.Manufacturer.BMW)) print(type(Plane.Manufacturer.AIRBUS)) 

Output for both is:

<enum 'Manufacturer'> <enum 'Manufacturer'> 

Although it does know about the type what it needs to know and makes comparisons appropriately

print(type(Car.Manufacturer.BMW) == type(Plane.Manufacturer.AIRBUS)) # is false as it should be 
2
  • 1
    I'd probably just change Manufacturer to CarManufacturer and PlaneManufacturer, respectively. Commented Apr 19, 2024 at 12:01
  • I assume that just using different names isn't an acceptable solution, since then it wouldn't be addressing the issue head-on. The only way to tell the same names apart is the context (namespace) that they're in, which in this case results from which classes those enums are nested within. In Python we get this information using the __qualname__ attribute of the enum (since it is still a nested class and has all the usual attributes of generic classes). Please see the linked duplicate for details. Commented Apr 19, 2024 at 13:00

2 Answers 2

1

Something like this would help:

class Car: class Manufacturer(IntEnum): BMW = 0 AUDI = 1 def __repr__(self): return f'<enum {type(self).__qualname__!r}>' 

To avoid repeating the __repr__ definition in each class, add your own intermediate Enum class or mixin from which you inherit your Manufacturers:

class QualEnum: def __repr__(self): return f'<enum {type(self).__qualname__!r}>' class Car: class Manufacturer(QualEnum, IntEnum): BMW = 0 AUDI = 1 
Sign up to request clarification or add additional context in comments.

Comments

1

The type() function in Python returns the type of an object. In your code, both Car.Manufacturer.BMWand Plane.Manufacturer.AIRBUS are members of an IntEnum That's why both type() calls output <enum 'Manufacturer'>.

If you need a clearer distinction between the two Manufacturer enums in terms of their type, you could consider the following options:

Option 1: Leverage the __qualname__ Attribute

The __qualname__ attribute exposes the fully qualified name of a class, including its parent class.

print(Car.Manufacturer.BMW.__class__.__qualname__) # Output: Car.Manufacturer print(Plane.Manufacturer.AIRBUS.__class__.__qualname__) # Output: Plane.Manufacturer 

Option 2: Define Enums at the Module Level

By defining enums outside of the classes, you avoid nesting and establish unique names.

from enum import IntEnum class CarManufacturer(IntEnum): BMW = 0 AUDI = 1 class PlaneManufacturer(IntEnum): AIRBUS = 0 BOEING = 1 class Car: pass class Plane: pass print(type(CarManufacturer.BMW)) # Output: <enum 'CarManufacturer'> print(type(PlaneManufacturer.AIRBUS)) # Output: <enum 'PlaneManufacturer'> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.