473,805 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6747

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.C 5137CF0
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.C 5137CF0--
Aug 27 '06 #5

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

Similar topics

4
3692
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 of the following kind: $people = array(); // maps age and e-mail address to names $people = array(21, "paul@foo.bar"); $people = array(22, "linda@bar.foo"); $people = array(19, "max@foobar.foobar");
6
2017
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 memory in terms of the length of the list i? If so why not and how could I change it to be so? I am sure the solution is O(n) since the list must only iterated once and the dictionary is O(1), correct? Thanks for the help!! #You are given a...
15
2064
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 strings of empty size "" I thought it would be a good idea to share en empty string allocation between all uninitialized strings. However I have no idea where to place this constant? :-S I tried this:
7
4588
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> int main() {
4
3918
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 is that the hashcode can change depending ion the clr version, from msdn 'The behavior of GetHashCode is dependent on its implementation, which might change from one version of the common language runtime to another. A reason why this might happen...
6
4914
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 this: public __value enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }; However, the resulting wsdl omits the actual flag values: - <s:simpleType name="Colors">
4
1744
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 the program. My view base on that if it doesn't that, you lose the memory of the string that you have used except store the address in a pointer, this is a form of memory leak although it doesn't really eat the memory as the general memory leak....
13
21857
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 will concatenate the value of ABC to MYSTR . I need this at compile time.
34
2176
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
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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,...
0
10604
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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
10103
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
9179
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
7644
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
6874
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();...

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.