473,690 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Possible constant assignment operators ":=" and "::=" for Python

Hi Pythonians,

To begin with I'd like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.

I have been missing constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very unintuitive. For the interpreter, in the
efficiency-wise, I just cannot tell.

For the solution I came up with two new assignment operators to the
language which would create a constant name and constant value: Let's
welcome the new constant assignment operators := and ::=

The := assignment operator says that "declare the name to be constant
and immutable". However, there might be some side-effects and the
constant may not always be constant as you will see below:

a = [1, 2, 3]
b := [4, 5, 6] # assign [4,5,6] to 'b' and declare name 'b' to
be "constant" and "immutable"
c := a # assign 'a' to 'c' and declare that name 'c' is
"constant" and "immutable"
d = c # assign 'c' to 'd' and 'd' inherits the
"immutable"-attribute from 'c', but 'd' can be assigned
later
e = d # assign 'd' to 'e' and 'e' inherits the
"immutable"-attribute from 'd', but 'e' can be assigned
later
a.append( b )
print a # prints [1, 2, 3, 4, 5, 6]
print c # prints [1, 2, 3, 4, 5, 6] because of the side-effect
print d # prints [1, 2, 3, 4, 5, 6] as well
print e # prints [1, 2, 3, 4, 5, 6] no magic here
c = b # 'c' cannot be assigned as 'c' is "constant"
c := b # 'c' cannot be redefined either as 'c' is
"constant"
c.append( 7 ) # cannot be done as 'c' is "immutable"
d.append( 7 ) # cannot be done as 'd' is "immutable" as it was
inherited from 'c'.
e.append( 7 ) # cannot be done as 'e' is "immutable" as it was
inherited from 'd'.
d = [7, 8, 9] # normal variable assignment because 'd' was not
declared to be a "constant"
d.append( 10 ) # now allowed as 'd' is not "immutable" any more

The ::= copy&assign-operator says that "make a copy of the
right-hand-side object and declare the name to be constant and
immutable ". This would give us a true constant object which cannot be
changed. Example follows:

a = [1, 2, 3]
b = [4, 5, 6]
c ::= a # make copy of 'a' and assign that new copy to 'c' and
declare that name 'c' is a "constant" and "immutable"
d = c # assign 'c' to 'd' and 'd' inherits the
"immutable"-attribute from 'c', but 'd' can be assigned
later
e := d # assign 'd' to 'e' and declare that name 'e' is
"constant" and "immutable"
a.append( b )
print a # prints [1, 2, 3, 4, 5, 6]
print c # prints [1, 2, 3] as 'a' and 'c' are two different
objects because of the ::=
print d # prints [1, 2, 3] no magic here
print e # prints [1, 2, 3] no magic here either
c = b # 'c' cannot be assigned as 'c' is "constant"
c := b # 'c' cannot be redefined either as 'c' is
"constant"
c.append( 7 ) # cannot be done as 'c' is "immutable"
d.append( 7 ) # cannot be done as 'd' is "immutable" as it was
inherited from 'c'.
e.append( 7 ) # cannot be done as 'e' is "immutable" as it was
inherited from 'd'.
d = [7, 8, 9] # normal variable assignment because 'd' was not
declared to be a "constant"
d.append( 10 ) # now allowed as 'd' is not "immutable" any more

The := operator would be computionally efficient as it creates only a
reference to an existing object but it may suffer from side-effects.
The ::= is less efficient as it makes a copy on an existing object, but
gives truly constant objects.

Does this make any sense to you, or are there some fatal issues that I
just happened to overlook?

Br,

T.S.

Apr 28 '06 #1
25 2566
ts*******@yahoo .com wrote:
To begin with I'd like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.

I have been missing constants in Python language. There are some
workarounds available, for example the const-module. To me, this looks
quite cumbersome and very unintuitive. For the interpreter, in the
efficiency-wise, I just cannot tell.


The question is, why have you been missing them? Constants serve a
variety of purposes in other languages, some of which might not be
worthwhile in Python. There was a thread about 'symbols' a while back
which covers many of the uses of constants.

--
Ben Sizer

May 2 '06 #2
ts*******@yahoo .com a écrit :
Hi Pythonians,

To begin with I'd like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.

I have been missing constants in Python language.


Why so ?

I guess you're talking about named (symbolic) constants ? If so, just
follow the convention : a name in ALL_UPPERCASE is a constant (or at
least will be treated as such by anyone not wanting to mess with your
package's implementation) . No need to add extra syntax here IMHO.
May 2 '06 #3
Bruno Desthuilliers wrote:
ts*******@yahoo .com a écrit :
Hi Pythonians,

To begin with I'd like to apologize that I am not very experienced
Python programmer so please forgive me if the following text does not
make any sense.

I have been missing constants in Python language.


Why so ?

I guess you're talking about named (symbolic) constants ? If so, just
follow the convention : a name in ALL_UPPERCASE is a constant (or at
least will be treated as such by anyone not wanting to mess with your
package's implementation) . No need to add extra syntax here IMHO.


Hi Bruno,

For example:
A = [] # let's declare a "constant" here
b = A # and let's assign the constant here
b.append('1') # OOPS!
c = A
print A ['1'] print b ['1'] print c

['1']

As you can see, the "constant" A can be modified this easily. But if
there were an intuitive mechanism to declare a symbol to be immutable,
then there won't be this problem.

Br,

- T.S.

May 3 '06 #4
ts*******@yahoo .com wrote:
For example:
A = [] # let's declare a "constant" here
b = A # and let's assign the constant here
b.append('1') # OOPS!
c = A
print A ['1'] print b ['1'] print c ['1']

As you can see, the "constant" A can be modified this easily. But if
there were an intuitive mechanism to declare a symbol to be immutable,
then there won't be this problem.


why are you using mutable objects as constants?

(insert obligatory "it hurts when I do this" joke here)

btw, given a hypothetical "object that symbol points to is immutable" syntax,
what would you expect this to do ?
constant A = [] b = [A] # much later
b[0].append('1')


</F>

May 3 '06 #5
Yes, I know that "constant" A will also be modified as the b[0] points
to A. Obviously the [] should be marked as immutable, as A is declared
to be constant thus immutable. If somebody tries to modify this
immutable object an error would occur.

When I further thought about this problem with constant objects (and
values), I run into this scenario: What if I want to use a constant
object/value as a template (or predefined value/class) for a variable:

constant A = ['1'] # let's declare A as immutable constant value/object
b = A # let's assign b some default value
b.append('2') # and let's play with b, but I wouldn't want to change A

What I'd like to see here is that b gets a copy of A so that the
original A won't be modified as we play with b. However, as we assign a
constant value A to b, I wouldn't want to restrict myself from playing
with b. Of course, I could write something like b = list(A) to get a
copy of A assigned to b. However, in this situation I have to know the
class name of A. But this is something that I would no like to have to
know if we want to take modules as some kind of black boxes.

Br,

T.S.

May 3 '06 #6
ts*******@yahoo .com wrote:
Yes, I know that "constant" A will also be modified as the b[0] points
to A. Obviously the [] should be marked as immutable, as A is declared
to be constant thus immutable. If somebody tries to modify this
immutable object an error would occur.


so a constant declaration doesn't only affect the namespace, it also modifies
the type of the object ?

are you sure you know how Python's object model work ? if you do, please
explain your proposal in terms of what needs to be changed, rather than in
terms of wishful thinking.

</F>

May 3 '06 #7
> are you sure you know how Python's object model work ? if you do, please
explain your proposal in terms of what needs to be changed, rather than in
terms of wishful thinking.


No, I do not know. As stated in my first post, I am quite newbie in
Python and miss a simple and intuitive mechanism that would allow to
declare something as constant and that would protect these "constant"
objects from accidental modifications.

T.S.

May 3 '06 #8
Fredrik Lundh a écrit :
ts*******@yahoo .com wrote:

For example:

>A = [] # let's declare a "constant" here
>b = A # and let's assign the constant here
>b.append(' 1') # OOPS!
>c = A
>print A


['1']
>print b


['1']
>print c


['1']

As you can see, the "constant" A can be modified this easily. But if
there were an intuitive mechanism to declare a symbol to be immutable,
then there won't be this problem.

why are you using mutable objects as constants?

(insert obligatory "it hurts when I do this" joke here)

btw, given a hypothetical "object that symbol points to is immutable" syntax,
what would you expect this to do ?
>>> constant A = [] >>> b = [A] >>> # much later
>>> b[0].append('1')
That's easy, since A is a symbolic constant know at compile time, and
since it's a known mutable objet, the code once compiled will be
equivalent to:
b = [[]] # much later
b|0].append('1')

May 3 '06 #9
ts*******@yahoo .com wrote:
As stated in my first post, I am quite newbie in
Python and miss a simple and intuitive mechanism that would allow to
declare something as constant and that would protect these "constant"
objects from accidental modifications.

T.S.


Python solution is to rely on the intelligence of programmers. If they
see
an all caps name and then they try to change it without knowing what
they are doing,
then they are stupid. If you have stupid programmers there is no way
the
language can stop them for making disasters.
For true constants, this is the end of the story. OTOH, sometimes you
want
read-only attributes which should not be accidentally overwritten but
that
are not really constants. In this case, the solution is to use
properties.
Just google the newsgroup for "properties " and you will find many
examples
of usage.

Michele Simionato

May 3 '06 #10

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

Similar topics

2
1550
by: Stephan Br?nnimann | last post by:
Dear all thanks to Scott Meyers (ME C++, item 33) we know assignment operators should be protected in the base class. Is there a common pattern (similar to virtual construction via `clone()') that allows me to have the following: (std::auto_ptr and covariant returns omitted for simplicity.) class Base {
2
19558
by: Todd Nathan | last post by:
Hi. have this code and compiler problem. GCC 2.95.3, BeOS, error "initializer element is not constant" #ifdef FILEIO { static struct { char *sfn; FILE *sfd; } stdfiles = {
4
2902
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to SignalMask:s. The reason is I have a free function called WaitSignal that accepts av SignalMask where Signals parameters are supposed to implicitly be converted to SignalMask:s. I'm using the SignalMask class because I want to be able to supply a logic...
1
2989
by: emin.martinian | last post by:
When trying to compile python extensions written in C using "python setup.py build" on cygwin I get the following error: foo.c: initializer element is not constant foo.c: error: (near initialization for `FooType.ob_type') I remember someone telling me a long time ago that this had something to do with declspec and how dlls are imported on cygwin/python. Can someone give me a pointer to how to build python extensions on cygwin?
32
14670
by: Licheng Fang | last post by:
Basically, the problem is this: 'do' Python's NFA regexp engine trys only the first option, and happily rests on that. There's another example: 'oneself' The Python regular expression engine doesn't exaust all the
4
1383
by: Leonid | last post by:
Hello all :) could anyone give a guide, ebook etc.. about the operators "?" and ":" thanks in advance Leonid
5
2029
bartonc
by: bartonc | last post by:
By that I mean they stop evaluation at the first True part of the expression. I use it often to replace an uninitialized object. Like this: >>> aString = "" >>> aString or "Hello, world" 'Hello, world' >>> anObject = None >>> anObject or range(5) >>> anObject = dict(a=1) >>> anObject or range(5)
1
8786
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
8793
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
7623
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
6457
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
5811
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
4322
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2965
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
2
2225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1952
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.