[Tutor] Re: c to python
Alan Gauld
alan.gauld@blueyonder.co.uk
Thu May 29 19:10:01 2003
> What if we have a statement like
>
> #define ABC 0x0001 in my c struct.
First thing to remember is that #define is really an
instruction to the C preprocessor to textually
substitute 0x0001 everytime you type ABC in a source
file. Its not really a language issue.
In particular since ANSI C introduced the const statement
these kind of #defines should be considered deprecated.
So looking at the issue of how we would represent
const int ABC=0x0001;
in Python, the most common technique is simply to define
a variable:
ABC = 0x0001
or just
ABC = 1
Then use it as usual (The uppercase name is just a convention
originating in C's similar convention for #defines)
In a bigger program you might like to put all such constants
in a module, in which case you can then do:
import consts
print consts.ABC
> I m following like this in python.
>
> class AB:
> def __init__(self):
> self.a = None
> self.b = None
> #statement here ?
Or yes, you could put the constants in a class.
Inwhich case you just add a line
self.ABC = 1
And access it as:
ab = AB()
print ab.ABC
HTH,
Alan G.