เรียกคลาส IntEnum จากในพจนานุกรม

Aug 18 2020

ฉันมีตัวอย่างคลาส 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, หรือsquareNONE

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)จำเป็นต้องใช้ตัวพิมพ์ใหญ่หรือไม่?


สิ่งที่ฉันได้ดูจนถึงตอนนี้:

เอกสารสำหรับงูหลามจะช่วยให้จำนวนตัวอย่าง; อย่างไรก็ตามไม่มีอะไรที่เหมาะกับกรณีของฉัน

คำตอบ

4 bad_coder Aug 18 2020 at 00:51

วิธีหนึ่งในการใช้งานจะมีดังต่อไปนี้:

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 เนื่องจากจะเป็นค่าคงที่ อย่างไรก็ตามผู้คนจำนวนมากตัดสินใจใช้ตัวพิมพ์ใหญ่เพียงตัวอักษรตัวแรกและเป็นตัวเลือกสไตล์ที่ถูกต้อง ตัวเลือกที่ใช้กันทั่วไปน้อยที่สุดคือใช้ตัวพิมพ์เล็กทั้งหมดหรือผสมตัวพิมพ์ใหญ่ที่แตกต่างกันอย่างไรก็ตามไม่มีอะไรหยุดคุณจากการทำ ดูคำตอบนี้สำหรับการอ้างอิงที่สมบูรณ์

3 EthanFurman Aug 18 2020 at 00:50
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.')
2 Jab Aug 18 2020 at 00:23

คุณต้องตรวจสอบself.shape.valueinsead self.shapeเมื่อต้องการตรวจสอบสตริงพื้นฐานในคลาส enum

if self.shape.value == 'circle':

หรือ

if self.shape == ShapeMethod.circle