473,396 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Defining constant strings

Hi,

I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.

Is this really not possible in Python?

Hans
Aug 27 '06 #1
4 6719

Hans wrote:
Hi,

I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.

Is this really not possible in Python?

Hans
It is really not possible.

The Pythonic way (as far as I have come to know it) is to stop trying
to protect yourself from yourself, and just don't change something
which should be constant. Good documentation (including in-code
comments) and good design are better for preventing programming errors
than defining things as constant.

Actually, you can't really define anything as constant in C either. A
#define can be overriden, a 'const' variable can be casted. These are
merely conventions which are enforced to some degree by compilers, but
can easily be worked around.

There are conventions for constant values in Python too. Usually,
variables with an all uppercase name are used. As for variables,
functions, attributes, methods etc. which shouldn't be changed/used
outside a certain module/class: these are usually prefixed with an
underscore ("_"). These are not enforced by the interpreter at all,
though.
As a side note, Python strings are actually all constants, or
"immutable" in Python-ish. The variables which reference string objects
can be changed to reference any other object - that's the nature of
Python variables. But the strings themselves don't change.

- Tal

Aug 27 '06 #2
Hans wrote:
Hi,

I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.

Is this really not possible in Python?

Hans
One last note:

If you truly insist on having constant variables, you could write a
class which implements such behavior. You can write a simple class
which answers your specific needs quite easily.

Or you could go for a more generic approach, such as this:
http://aspn.activestate.com/ASPN/Coo...n/Recipe/59878

- Tal

Aug 27 '06 #3
Hans wrote:
I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.

Is this really not possible in Python?
No, this is not really not possible in Python:

$ ls
preprocess.pyp
$ cat preprocess.pyp
#define MYSTRING "Hello, world"
def f():
print "Goodbye, " MYOTHERSTRING
print MYSTRING
f()
$ gcc -DMYOTHERSTRING="'sanity'" -xc -E preprocess.pyp -o preprocess.py
$ python preprocess.py
Hello, world
Goodbye, sanity
$

:-)

Peter
Aug 27 '06 #4
I would really like to highlight something Tal has already said: Python
strings are immutable. That means if you construct a string object, you
don't have to worry about someone else going in and changing that
object. What might happen, however, is that someone might reassign a
variable you have which points to that object. You can tell people not
to do this (although you can't force them) using a mechanism Tal has
also described. You give your variable a name which is all caps.

Hans wrote:
Hi,

I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.

Is this really not possible in Python?

Hans
------=_NextPart_000_004E_01C6C9FF.C5137CF0
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: quoted-printable
X-Google-AttachSize: 902

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.2900.2604" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#e6e3df>
<DIV><FONT face=Arial size=2>Hi,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>I want to define a couple of constant strings, like
in C:</FONT></DIV>
<DIV><FONT face=Arial size=2>#define mystring "This is my string"</FONT></DIV>
<DIV><FONT face=Arial size=2>or using a const char construction.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Is this really not possible in Python?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
<DIV><FONT face=Arial size=2>Hans</FONT></DIV></BODY></HTML>

------=_NextPart_000_004E_01C6C9FF.C5137CF0--
Aug 27 '06 #5

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

Similar topics

4
by: Christian Hackl | last post by:
I honestly wasn't able to find an answer for this design question using Google and Google Groups, so I apologize if it is asked too frequently :) Anyway: Let's say I have a multidimensional array...
6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
15
by: Lasse Skyum | last post by:
I have my own string class that I'm generally verry fond of compared to std::string. For that same reason I'm trying to improve a little on it. Since manny objects in my project will contain...
7
by: Dave Ohlsson | last post by:
Hi, In ISO C/C++, a string constant prefixed by the letter `L' is a wide string constant and is of type "array of wchar_t". Consider the following C program fragment: #include <stdio.h>...
4
by: Nick | last post by:
Hi all, I am using GetHashCode on unique strings to get a unique integer for a string that I can then place into a database (use int rather than the string to make indexing faster). The problem...
6
by: cipher | last post by:
I have some constant values in my web service that my client application will require. Having to keep server side and client side definitions insync is tedious. I am trying to do something like...
4
by: Quentin Yuan | last post by:
I always consider that the constant character strings of which literal value are the same lay out at the same logic address, in another words, every constant character string have only one copy in...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
34
by: jacob navia | last post by:
Hi I am adding an optimization to lcc-win: sqrt(2.0) will provoke now that the constant 1.4142... etc will be generated instead of generating an actual call. Details: -------
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.