mirror of
				https://github.com/python/cpython.git
				synced 2025-10-30 18:07:37 +00:00 
			
		
		
		
	Close issue25594: advise against accessing Enum members from other members
This commit is contained in:
		
							parent
							
								
									875e4fbcca
								
							
						
					
					
						commit
						92e5d2f0eb
					
				
					 1 changed files with 15 additions and 9 deletions
				
			
		|  | @ -730,18 +730,24 @@ member instances. | ||||||
| Finer Points | Finer Points | ||||||
| ^^^^^^^^^^^^ | ^^^^^^^^^^^^ | ||||||
| 
 | 
 | ||||||
| Enum members are instances of an Enum class, and even though they are | :class:`Enum` members are instances of an :class:`Enum` class, and even | ||||||
| accessible as `EnumClass.member`, they are not accessible directly from | though they are accessible as `EnumClass.member`, they should not be accessed | ||||||
| the member:: | directly from the member as that lookup may fail or, worse, return something | ||||||
|  | besides the :class:`Enum` member you looking for:: | ||||||
| 
 | 
 | ||||||
|     >>> Color.red |     >>> class FieldTypes(Enum): | ||||||
|     <Color.red: 1> |     ...     name = 0 | ||||||
|     >>> Color.red.blue |     ...     value = 1 | ||||||
|     Traceback (most recent call last): |     ...     size = 2 | ||||||
|     ... |     ... | ||||||
|     AttributeError: 'Color' object has no attribute 'blue' |     >>> FieldTypes.value.size | ||||||
|  |     <FieldTypes.size: 2> | ||||||
|  |     >>> FieldTypes.size.value | ||||||
|  |     2 | ||||||
| 
 | 
 | ||||||
| Likewise, the :attr:`__members__` is only available on the class. | .. versionchanged:: 3.5 | ||||||
|  | 
 | ||||||
|  | The :attr:`__members__` attribute is only available on the class. | ||||||
| 
 | 
 | ||||||
| If you give your :class:`Enum` subclass extra methods, like the `Planet`_ | If you give your :class:`Enum` subclass extra methods, like the `Planet`_ | ||||||
| class above, those methods will show up in a :func:`dir` of the member, | class above, those methods will show up in a :func:`dir` of the member, | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Ethan Furman
						Ethan Furman