Skip to main content
Became Hot Network Question

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"
    
class Blue:
    name = "blue"
    
class Colors(Enum):
    red = Red
    blue = Blue
    
    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()

print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically
class Red:
    name = "red"
    
class Blue:
    name = "blue"
    
class Colors(Enum):
    red = Red
    blue = Blue
    
    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()

print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]
deleted 7 characters in body
Source Link
Alex
  • 121
  • 1
  • 5

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there should be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there should be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]
Source Link
Alex
  • 121
  • 1
  • 5

Factory pattern using enum

I wanted to have a factory-method, but without the if statements that are often seen in examples of this pattern. So I came up with this, and would like to know: is this a right approach, should there should be any impovements?

some things I considered:

  • It is using Enum because that makes it possible to iterate or list the possible options.
  • using getattr to call the class dynamically

.

class Red:
    name = "red"

class Blue:
    name = "blue"

class Colors(Enum):
    red = Red
    blue = Blue

    @classmethod
    def factory(cls, name):
        if any(e.name == name for e in cls):
            target = cls[name]
        else:
            #default
            target = cls.blue
        return getattr(sys.modules[__name__], target.value.__name__)()


print(Colors.factory("red"))
print(Colors.factory("red"))
print(Colors.factory("red2"))
print(list(Colors))
### result
<__main__.Red object at 0x7fd7cc2e2250>
<__main__.Red object at 0x7fd7cc2e2430>
<__main__.Blue object at 0x7fd7cc30c700>
[<Colors.red: <class '__main__.Red'>>, <Colors.blue: <class '__main__.Blue'>>]