एक शब्दकोश के अंदर से एक IntEnum वर्ग को बुलाओ
मेरे पास एक उदाहरण है IntEnum वर्ग:
class ShapeMethod(IntEnum):
NONE = 0
circle = 1
square = 2
जिसे __init__
दूसरे वर्ग के कार्य द्वारा बुलाया जाना चाहिए :
class ExampleClass():
def __init__(look_at_shapes=None):
"""
Args:
look_at_shapes (dict): A dictionary of shape inputs.
"""
if look_at_shapes:
self.shape = ShapeMethod.NONE
if look_at_shapes["colour"]:
self.colour = look_at_shapes["colour"]
def do_something_with_shape:
if self.shape == ShapeMethod.circle:
print('awesome you got a circle'.)
elif self.shape == ShapeMethod.square:
print('squares have 4 sides.')
else:
print('nothing to see here.')
जहां, self.shape
विशेषता को होना चाहिए circle
, square
या NONE
।
तब do_something_with_shape
समारोह द्वारा बुलाया जाएगा:
input = {"colour" = blue}
my_object = ExampleClass(look_at_shape=input)
my_object.do_something_with_shape
input
एक शब्दकोश होने की जरूरत की संरचना , और इसे सेट करने के लिए यथोचित सरल है colour
। हालांकि, मुझे नहीं पता कि IntEnum
किसी शब्दकोश के अंदर से कक्षा का सही उपयोग कैसे किया जाए । उदाहरण के लिए, यदि मैं प्रिंट करना चाहता हूंsquares have 4 sides.
Note: Do all options in ShapeMethod(IntEnum)
need to be capitalized?
What I have looked at so far:
The documentation for Python gives a number of examples; however, none fit my exact case.
जवाब
One way to use would be the following:
from enum import IntEnum
class ShapeMethod(IntEnum):
NONE = 0
circle = 1
square = 2
class ExampleClass:
def __init__(self, look_at_shapes=None):
"""
Args:
look_at_shapes (dict): A dictionary of shape inputs.
"""
self.shape = None
self.colour = None
if look_at_shapes:
if look_at_shapes[ShapeMethod]:
self.shape = look_at_shapes[ShapeMethod]
if look_at_shapes["colour"]:
self.colour = look_at_shapes["colour"]
def do_something_with_shape(self):
if self.shape is ShapeMethod.NONE:
print('awesome you got a circle.')
elif self.shape is ShapeMethod.square:
print('squares have 4 sides.')
else:
print('nothing to see here.')
input_var = {
"colour": 'blue',
ShapeMethod: ShapeMethod.square
}
my_object = ExampleClass(look_at_shapes=input_var)
my_object.do_something_with_shape()
Regarding your side question:
Note: Do all options in
ShapeMethod(IntEnum)
need to be capitalized?
It's entierly up to you. They can be all upper-case, which is in acordance with PEP 8 since they'll be constants. However, a lot of folks decide to capitalize only the first letter and it's a valid choice of style. The least common option is using all lower-case or mixing different capitalizations, however there is nothing stopping you from doing so. See this answer for a complete reference.
input = {
"shape": ShapeMethod.square,
"colour": "blue",
}
def __init__(look_at_shapes=None):
"""
Args:
look_at_shapes (dict): A dictionary of shape inputs.
"""
look_at_shapes = look_at_shapes or {}
self.shape = look_at_shapes.get("shape", ShapeMethod.NONE)
self.colour = look_at_shapes.get("colour") # None if "colour" doesn't exist
def do_something_with_shape:
if self.shape == ShapeMethod.circle:
print('awesome you got a circle'.)
elif self.shape == ShapeMethod.square:
print('squares have 4 sides.')
else:
print('nothing to see here.')
You need to check self.shape.value
insead of self.shape
when you're looking to check the underlying string in the enum class.
if self.shape.value == 'circle':
Or
if self.shape == ShapeMethod.circle