เรียกคลาส 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.
หมายเหตุ: ตัวเลือกทั้งหมดShapeMethod(IntEnum)
จำเป็นต้องใช้ตัวพิมพ์ใหญ่หรือไม่?
สิ่งที่ฉันได้ดูจนถึงตอนนี้:
เอกสารสำหรับงูหลามจะช่วยให้จำนวนตัวอย่าง; อย่างไรก็ตามไม่มีอะไรที่เหมาะกับกรณีของฉัน
คำตอบ
วิธีหนึ่งในการใช้งานจะมีดังต่อไปนี้:
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()
เกี่ยวกับคำถามด้านข้างของคุณ:
หมายเหตุ: ตัวเลือกทั้งหมด
ShapeMethod(IntEnum)
จำเป็นต้องใช้ตัวพิมพ์ใหญ่หรือไม่?
มันขึ้นอยู่กับคุณ อาจเป็นตัวพิมพ์ใหญ่ทั้งหมดซึ่งสอดคล้องกับ PEP 8 เนื่องจากจะเป็นค่าคงที่ อย่างไรก็ตามผู้คนจำนวนมากตัดสินใจใช้ตัวพิมพ์ใหญ่เพียงตัวอักษรตัวแรกและเป็นตัวเลือกสไตล์ที่ถูกต้อง ตัวเลือกที่ใช้กันทั่วไปน้อยที่สุดคือใช้ตัวพิมพ์เล็กทั้งหมดหรือผสมตัวพิมพ์ใหญ่ที่แตกต่างกันอย่างไรก็ตามไม่มีอะไรหยุดคุณจากการทำ ดูคำตอบนี้สำหรับการอ้างอิงที่สมบูรณ์
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.')
คุณต้องตรวจสอบself.shape.value
insead self.shape
เมื่อต้องการตรวจสอบสตริงพื้นฐานในคลาส enum
if self.shape.value == 'circle':
หรือ
if self.shape == ShapeMethod.circle