Python -- OOP高级 -- 枚举类
Enum可以把一组相关常量定义在一个class中,且class不可变,而且成员可以直接比较。
from enum import Enum
Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', \
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'))
for name, member in Month.__members__.items():
print("%s => %s, %d" % (name, member, member.value))
>>>
Jan => Month.Jan, 1
Feb => Month.Feb, 2
Mar => Month.Mar, 3
Apr => Month.Apr, 4
May => Month.May, 5
Jun => Month.Jun, 6
Jul => Month.Jul, 7
Aug => Month.Aug, 8
Sep => Month.Sep, 9
Oct => Month.Oct, 10
Nov => Month.Nov, 11
Dec => Month.Dec, 12
Python -- OOP高级 -- 枚举类的更多相关文章
- Python -- OOP高级 -- 元类
type()函数既可以返回一个对象的类型,又可以创建出新的类型 def fn(self, name="world"): print("Hello, %s!" % ...
- Python -- OOP高级 -- 定制类
__str__ 和 __repr__ :实例对象直接显示字符串 class Student: def __init__(self, name): self.name = name def __str_ ...
- Python中使用枚举类
开发中我们经常定义常量, 其实有更好的方法:为这样的枚举类型定义一个class类型,然后,每个常量都是class的一个唯一实例.Python中提供了Enum类来实现这个功能: from enum im ...
- 【python】使用枚举类
当我们需要定义常量时,一个办法是用大写变量通过整数来定义,例如月份: JAN = 1 FEB = 2 MAR = 3 ... NOV = 11 DEC = 12 好处是简单,缺点是类型是int,并且仍 ...
- Python -- OOP高级 -- __slots__、@property
__slots__属性可以设置 允许被设置的属性 class Student: __slots__ = ("name", "age") >>> ...
- python 面向对象十三 枚举类
from enum import Enum Month = Enum('Month', ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', ...
- Python OOP(1):从基础开始
本文旨在Python复习和总结: 1.如何创建类和实例? # 创建类 class ClassName(object): """docstring for ClassNam ...
- <转>Python OOP(1):从基础开始
转自 http://www.cnblogs.com/BeginMan/p/3510786.html 本文旨在Python复习和总结: 1.如何创建类和实例? # 创建类 class ClassNam ...
- Python面向对象高级编程-__slots__、定制类,枚举
当在类体内定义好各种属性后,外部是可以随便添加属性的,Python中类如何限制实例的属性? Python自带了很多定制类,诸如__slots__,__str__ __slots__ __slots__ ...
随机推荐
- 使用solrj查询数据(java代码)
实体类Student,添加Field注解 package com.cs.solr.entity; import org.apache.solr.client.solrj.beans.Field; pu ...
- 关于解决“No matching provisioning profiles found”问题-ios
xcode7之后真机调试就可以不需要调试证书了,但其中也会遇到一些问题令人挠头搔耳.记录下来是给自己提供方便,也为初遇到此问题的人提供解答,利人利己的事情我做! 上图: 图一 本人有一种视警号为e ...
- 2.定义图形类Shape,该类中有获得面积的方法getArea();定义长方形类Rect,该类是Shape的子类,类中有矩形长和宽的变量double a,double b,设置长和宽的方法setWidth()、setHeight(),使用getArea()求矩形面积;利用getArea方法实现题1中圆面积的求解。
// 图形类Shape package d922B; public class Shape { double getArea(ShapePara x){ return x.getArea(); } d ...
- yii2.0使用ActionForm创建表单
文本框:textInput(); 密码框:passwordInput(); 单选框:radio(),radioList(); 复选框:checkbox(),checkboxList(); 下拉框:dr ...
- mac 文件路径问题
Finder栏显示访问路径 操作步骤: 打开“终端”(应用程序->实用工具),输入以下两条命令: defaults write com.apple.finder _FXShowPosixPath ...
- mybatis----增删改查
转: select使用 : xml代码: <!-- 查询学生,根据id --> <select id="getStudent" parameterType=&qu ...
- 还原openstack配置文件的方法
cp -a /etc/neutron/neutron.conf /etc/neutron/neutron.conf.bakcp -a /etc/neutron/plugins/ml2/ml2_conf ...
- Tomcat目录下文件详解
一.Tomcat背景 (转) 自从JSP发布之后,推出了各式各样的JSP引擎.Apache Group在完成GNUJSP1.0的开发以后,开始考虑在SUN的JSWDK基础上开发一个可以直接提供We ...
- LeetCode OJ 109. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- LeetCode OJ 220.Contains Duplicate 3
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...