Return 来自 Python Class 的所有 class 个变量值
Return all class variable values from a Python Class
我有一个包含所有 AWS 区域的 Python class。我写了一个 class 方法,可以 return 我得到所有地区的列表。有没有更好的方法 returning 所有 class 变量值,这样我就不必像我在下面的示例中那样对 return 语句中的所有值进行硬编码?
class AwsRegion():
'''
Class to define AWS Regions
'''
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
@classmethod
def all(cls, ):
return [AwsRegion.OHIO, AwsRegion.NORTH_VIRGINIA, AwsRegion.NORTH_CALIFORNIA, AwsRegion.OREGON, \
AwsRegion.MUMBAI, AwsRegion.SEOUL, AwsRegion.SINGAPORE, AwsRegion.SYDNEY, AwsRegion.TOKYO, \
AwsRegion.FRANKFURT, AwsRegion.IRELAND, AwsRegion.LONDON, AwsRegion.SAO_PAULO]
在这种情况下,您可以枚举class中所有大写的属性;我将使用 vars()
function 访问 class 命名空间:
@classmethod
def all(cls):
return [value for name, value in vars(cls).items() if name.isupper()]
演示:
>>> class AwsRegion():
... '''
... Class to define AWS Regions
... '''
... OHIO = 'us-east-2'
... NORTH_VIRGINIA = 'us-east-1'
... NORTH_CALIFORNIA = 'us-west-1'
... OREGON = 'us-west-2'
... MUMBAI = 'ap-south-1'
... SEOUL = 'ap-northeast-2'
... SINGAPORE = 'ap-southeast-1'
... SYDNEY = 'ap-southeast-2'
... TOKYO = 'ap-northeast-1'
... FRANKFURT = 'eu-central-1'
... IRELAND = 'eu-west-1'
... LONDON = 'eu-west-2'
... SAO_PAULO = 'sa-east-1'
... @classmethod
... def all(cls):
... return [value for name, value in vars(cls).items() if name.isupper()]
...
>>> AwsRegion.all()
['us-east-2', 'us-east-1', 'us-west-1', 'us-west-2', 'ap-south-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'sa-east-1']
作为一般参考,您可以通过以下方式获取任何class的属性。
根据需要选择:
将return所有可写class属性的字典。这通常是您所需要的。
my_obj = MyClass()
attributes = my_obj.__dict__
与 __dict__
相同的结果,但使用它被认为是最佳实践。
my_obj = MyClass()
attributes = vars(my_obj)
Will return 所有 class 属性,包括那些不是由您创建但继承自 object
.
my_obj = MyClass()
attributes = dir(my_obj)
对于您的情况,使用 vars()
会很好,正如 Martijn Pieters 在他的 中所展示的那样。
看起来您正在做的可能是不小心创建了一个 Enum
类型。
如果您的 AwsRegion
class 仅用于存储这些值(而不是许多其他复杂行为),请尝试将其设为 Enum
的子 class。这将为您提供一些不错的方法,而无需您自己重新创建所有方法,并使您的代码对于知道什么是枚举类型的其他人来说更清晰。
from enum import Enum
class AwsRegion2(Enum):
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
print(list(AwsRegion2))
结合上述答案,我认为这样效率更高:
from enum import Enum
class AwsRegion2(Enum):
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
@classmethod
def all(cls):
return [variable.value for variable in list(cls)]
print(AwsRegion2.all())
我有一个包含所有 AWS 区域的 Python class。我写了一个 class 方法,可以 return 我得到所有地区的列表。有没有更好的方法 returning 所有 class 变量值,这样我就不必像我在下面的示例中那样对 return 语句中的所有值进行硬编码?
class AwsRegion():
'''
Class to define AWS Regions
'''
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
@classmethod
def all(cls, ):
return [AwsRegion.OHIO, AwsRegion.NORTH_VIRGINIA, AwsRegion.NORTH_CALIFORNIA, AwsRegion.OREGON, \
AwsRegion.MUMBAI, AwsRegion.SEOUL, AwsRegion.SINGAPORE, AwsRegion.SYDNEY, AwsRegion.TOKYO, \
AwsRegion.FRANKFURT, AwsRegion.IRELAND, AwsRegion.LONDON, AwsRegion.SAO_PAULO]
在这种情况下,您可以枚举class中所有大写的属性;我将使用 vars()
function 访问 class 命名空间:
@classmethod
def all(cls):
return [value for name, value in vars(cls).items() if name.isupper()]
演示:
>>> class AwsRegion():
... '''
... Class to define AWS Regions
... '''
... OHIO = 'us-east-2'
... NORTH_VIRGINIA = 'us-east-1'
... NORTH_CALIFORNIA = 'us-west-1'
... OREGON = 'us-west-2'
... MUMBAI = 'ap-south-1'
... SEOUL = 'ap-northeast-2'
... SINGAPORE = 'ap-southeast-1'
... SYDNEY = 'ap-southeast-2'
... TOKYO = 'ap-northeast-1'
... FRANKFURT = 'eu-central-1'
... IRELAND = 'eu-west-1'
... LONDON = 'eu-west-2'
... SAO_PAULO = 'sa-east-1'
... @classmethod
... def all(cls):
... return [value for name, value in vars(cls).items() if name.isupper()]
...
>>> AwsRegion.all()
['us-east-2', 'us-east-1', 'us-west-1', 'us-west-2', 'ap-south-1', 'ap-northeast-2', 'ap-southeast-1', 'ap-southeast-2', 'ap-northeast-1', 'eu-central-1', 'eu-west-1', 'eu-west-2', 'sa-east-1']
作为一般参考,您可以通过以下方式获取任何class的属性。
根据需要选择:
将return所有可写class属性的字典。这通常是您所需要的。
my_obj = MyClass()
attributes = my_obj.__dict__
与 __dict__
相同的结果,但使用它被认为是最佳实践。
my_obj = MyClass()
attributes = vars(my_obj)
Will return 所有 class 属性,包括那些不是由您创建但继承自 object
.
my_obj = MyClass()
attributes = dir(my_obj)
对于您的情况,使用 vars()
会很好,正如 Martijn Pieters 在他的
看起来您正在做的可能是不小心创建了一个 Enum
类型。
如果您的 AwsRegion
class 仅用于存储这些值(而不是许多其他复杂行为),请尝试将其设为 Enum
的子 class。这将为您提供一些不错的方法,而无需您自己重新创建所有方法,并使您的代码对于知道什么是枚举类型的其他人来说更清晰。
from enum import Enum
class AwsRegion2(Enum):
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
print(list(AwsRegion2))
结合上述答案,我认为这样效率更高:
from enum import Enum
class AwsRegion2(Enum):
OHIO = 'us-east-2'
NORTH_VIRGINIA = 'us-east-1'
NORTH_CALIFORNIA = 'us-west-1'
OREGON = 'us-west-2'
MUMBAI = 'ap-south-1'
SEOUL = 'ap-northeast-2'
SINGAPORE = 'ap-southeast-1'
SYDNEY = 'ap-southeast-2'
TOKYO = 'ap-northeast-1'
FRANKFURT = 'eu-central-1'
IRELAND = 'eu-west-1'
LONDON = 'eu-west-2'
SAO_PAULO = 'sa-east-1'
@classmethod
def all(cls):
return [variable.value for variable in list(cls)]
print(AwsRegion2.all())