Re: [Tutor] Enumeration and constant
> > how about custom constant type? > > Does python provide? > No. Python tries to be as "light" as possible. There is a convention > amongst Python programmers to use upper case names to represent > constants, e.g. RED = 3. another aspect of Python numbers that make them "constant" is that all numeric types are immutable, meaning you cannot change their values anyway. the idiom that Bob and Alan described is mostly for the programmer. you can, of course, reassign that variable to another number (which is also immutable), and Python won't stop you. i have used tuples/lists and dicts as a proxy for enum-type functionality, and they work just fine. also, do not get confused between all this and the enumerate() built-in function. all that does is for a sequence, return both an index and the corresponding sequence element so that folks stop doing stuff like "for i in range(len(seq))". hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumeration and constant
Please always reply to the list as well as me. Others may be able to help, and we all learn from our interactions. Brendan Cheng wrote: > how about custom constant type? > Does python provide? No. Python tries to be as "light" as possible. There is a convention amongst Python programmers to use upper case names to represent constants, e.g. RED = 3. -- Bob Gailer 510-978-4454 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumeration and constant
> I wander how to setup the enumeration type > and constant in Python, These concepts do not exist in Python per se. The convention is to make constant names uppercase: PI = 3.1415926 Enums are a little more variable in implementation depending on what you want to do with them. If you need to iterate over them a tuple might be a choice with strings as values. But if you need to use them as indices into a list a dictionary might be better.(But you could use them as keys into a dictionary instead of course!) Other more sophisticated options include creating classes. These can implement restricted ranges etc In practice I rarely find I need enums in Python because the wealth of data types (lists, tupoles, sets, dictionaries, classes) means I rarely need and extra symbolic values. I usually find myself using enums in other languages because I don't have quite the right data type available and an enum helps fake it. HTH, Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Enumeration and constant
Brendan Cheng wrote: I wander how to setup the enumeration type and constant in Python, which I couldn't find the topic in the help file. There is no built-in enumeration type. See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/67107 for an example of an enumeration class. -- Bob Gailer 510-978-4454 Broadband Phone Service for local and long distance $19.95/mo plus 1 mo Free ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Enumeration and constant
Hi, I wander how to setup the enumeration type and constant in Python, which I couldn't find the topic in the help file. I'm using python 2.4.3 please give me an example of it as well Thanks, Brendan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Enumeration and Constant
Hi, I wander how to setup the enumeration type and constant in Python, which I couldn't find the topic in the help file. I'm using python 2.4.3 please give me an example of it as well Thanks, Brendan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor