473,545 Members | 2,772 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 2546
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
1544
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
19544
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
2897
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...
1
2969
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...
32
14630
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
1377
by: Leonid | last post by:
Hello all :) could anyone give a guide, ebook etc.. about the operators "?" and ":" thanks in advance Leonid
5
2027
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)
0
7502
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...
0
7692
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. ...
0
7946
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...
1
5360
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...
0
5078
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...
0
3491
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...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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
1045
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.