473,597 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enum question

does python support a C-like enum statement where one can define a
variable with prespesified range of values?

thanks
m.smadi
Jul 18 '05 #1
4 1239
Not completely sure I understand (my C is weak).
But you can do:

x=range(9)

x's contents will be
[0,1,2,3,4,5,6,7 ,8]

or

x=range(12,25)

x's conetnst will be
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

if you mean a way to limit x's contents to a predefined
list of values, you need to write a class for that and
override the __setattr__ method to limit. Quick
example:

class a:
def __setattr__(sel f, attr, x):
if x in range(10): self.__dict__[attr]=x
else:
print "error, x not in ", str(range(10))
x=a()
x.value=11 error, x not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] x.value=8

Larry Bates
M.N.A.Smadi wrote: does python support a C-like enum statement where one can define a
variable with prespesified range of values?

thanks
m.smadi

Jul 18 '05 #2
M.N.A.Smadi wrote:
does python support a C-like enum statement where one can define a
variable with prespesified range of values?


Not built in, but there are various solutions available, some simpler
than others. See the Infrequently Asked Questions:
http://www.norvig.com/python-iaq.html

--
Steve Toledo-Brown
Speaking for myself only.
Humans please use domain uk.ibm.com
Jul 18 '05 #3
M.N.A.Smadi wrote:
does python support a C-like enum statement where one can define a
variable with prespesified range of values?

thanks
m.smadi

BLUE, RED, GREEN = 1,5,8
BLUE 1 RED 5 GREEN

8
Jul 18 '05 #4

M.N.A.Smadi wrote:
does python support a C-like enum statement where one can define a
variable with prespesified range of values?


The thing is, variables don't have types; objects do. A variable can
be bound to an object of any type, so there's no way to prespecify a
range of values for a variable.

Your question has the air of someone who's evaluating Python features,
considering whether to try it. If so, you might need to widen your
worldview a little to understand Python; its variables are
fundamentally different from C, and things like enums don't make much
sense in Python because of it. However, Python is versatile enough
that you can get something to that effect if you really need it.
--
CARL BANKS

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6479
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent? Thanks in advance... -Nikhil
5
2658
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer references the Data layer, but I would like to have the enum exposed to all three. Is there a way to trickle down that enum (Like class inheritance exposes classes) so that it can be accessed from the Business layer and UI layers? My UI layer doesn't...
6
2872
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I understand correctly, what is actually happening on the calling side is an instance of the enum class FavoriteSport is getting created and sent to the PrintFavorite routine with a default value of type int. Is this correct? Say I have a couple hundred uses...
5
274
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I understand correctly, what is actually happening on the calling side is an instance of the enum class FavoriteSport is getting created and sent to the PrintFavorite routine with a default value of type int. Is this correct? Say I have a couple hundred uses...
34
11161
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
0
7884
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8258
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6683
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5845
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5423
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3922
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2394
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1493
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.