0

I'm new to python so I don't really know a lot in this topic.

I have a function which gets item type, I have three types: A, B and C.

My initial approach was to pass item type as string, for example:

def my_function (item_type):
    if item_type == 'A': ...

But this is a bad approach, what are the alternatives? maybe class, but how to pass it to function

6
  • Are they classes? If so you can pass them directly Commented Aug 15, 2022 at 8:57
  • Can you clarify on the 'types' A B C? Are they custom types? Commented Aug 15, 2022 at 9:00
  • @TerryA like colors red green and blue. what are your suggestions? Commented Aug 15, 2022 at 9:06
  • I think you're looking for enums. Commented Aug 15, 2022 at 9:14
  • Now we know what your types are, what is in the ...? How does that vary depending on item_type? Commented Aug 15, 2022 at 9:22

3 Answers 3

3

Rather than working with strings, use enums. For example:

from enum import Enum

class Colour(Enum):
    RED = 0
    GREEN = 1
    BLUE = 2

If the colours need to be distinct then just make sure that the values assigned are all different.

Then you might have a function like this:

def func(colour):
    match colour:
        case Colour.RED:
            pass
        case Colour.GREEN:
            pass
        case Colour.BLUE:
            pass

Of course, the enum might be wrapped in some other class but this should get you started

Sign up to request clarification or add additional context in comments.

Comments

3

If you are checking instances of classes then you can use isinstance or check using the type function

class A:
   pass

a = A()
print(isinstance(a,A))  # this is the preferred method
print(type(a) == A)

OUTPUT

True
True

If you are checking the classes themselves then you can do a straight comparison.

class A:
    pass

print(A == A)

OUTPUT

True

If you are checking subclasses then you can use issubclass function

class Parent:
    pass

class A(Parent):
    pass

class B(Parent):
    pass

print(issubclass(A, Parent))
print(issubclass(B, Parent))

OUTPUT

True
True


Update from the question in your comment if you want to consolidate classes you could do something like this:

class Color:
    def __init__(self, color):
        self.color = color

red = Color('red')
blue = Color('blue')
green = Color('green')

print(red.color)
print(blue.color)
print(green.color)
print(isinstance(red, Color))
print(isinstance(green, Color))

OUTPUT

'red'
'blue'
'green'
True
True

4 Comments

Do I need 3 classes A,B,C? but all types are types of same object... they should be one
See my comment please, I still believe no need for 3 classes. For example I mean Red, Green and Blue (all colors)
Do you need all 3? It depends on what you plan to use them for.
@log see the bottom of my answer... all three are not needed
0

The good design usually avoids using isinstance() or type() calls. If you need some class-specific logic you better encapsulate it in a class itself. Please refer to: https://refactoring.guru/replace-conditional-with-polymorphism

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.