473,395 Members | 1,639 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,395 software developers and data experts.

Small Troll on notation of variables over time

Hi there,

I can write:

s = 'some string'
then print s[1] will be the string 'o'

and a while later I can write:

s = 'other some string'
then print s[1] will be the string 't'

and then:

s = [1,2,3,4]
then print s[1] will be the number 2

and still later:

s = {1:'boo',2:'foo',3:'shoo'}
when print s[1] will yield the string 'boo'

Now how about introducing an index that works over time,
such that s{0} (the default so as to not break any existing code)
implies the current object bound to the name s,
with s{1} being the previous one, and so on...

This will mean that s{2}[1] is the string 't'
and s{3}[1] is the string 'o'
while s{4} should be None...

It should be easy to implement this, as all that needs to be done is to
change the "pointer" (or whatever) to the object with a stack of them
at the time the binding or rebinding is done...

I first thought of using s{-1} for the previous "value" but that
suffers from the regrettable disadvantage that it implies the
existence of an s{1} - i.e. a future value - and I could not think
of a way to achieve this - so the minus sign adds no information
and should therefore be left out...

What do you guys think?

- Hendrik

Aug 19 '06 #1
7 1267

Hendrik van Rooyen wrote:
Hi there,

I can write:

s = 'some string'
then print s[1] will be the string 'o'

and a while later I can write:

s = 'other some string'
then print s[1] will be the string 't'

and then:

s = [1,2,3,4]
then print s[1] will be the number 2

and still later:

s = {1:'boo',2:'foo',3:'shoo'}
when print s[1] will yield the string 'boo'

Now how about introducing an index that works over time,
such that s{0} (the default so as to not break any existing code)
implies the current object bound to the name s,
with s{1} being the previous one, and so on...

This will mean that s{2}[1] is the string 't'
and s{3}[1] is the string 'o'
while s{4} should be None...

It should be easy to implement this, as all that needs to be done is to
change the "pointer" (or whatever) to the object with a stack of them
at the time the binding or rebinding is done...

I first thought of using s{-1} for the previous "value" but that
suffers from the regrettable disadvantage that it implies the
existence of an s{1} - i.e. a future value - and I could not think
of a way to achieve this - so the minus sign adds no information
and should therefore be left out...

What do you guys think?
I think it's pointless. If you want access to more than one of these
variables, don't use the same name.

That and if a symbol is still in scope, then all of its previous values
still have active references, so they won't get deallocated by the
garbage collector, meaning a lot of overhead that most people will not
take advantage of.

Aug 19 '06 #2

Hendrik van Rooyen wrote:
[snip]
What do you guys think?
The subject said it all. You should find some other way of entertaining
yourself on the weekends :-)

Aug 19 '06 #3

"John Machin" <sj******@lexicon.netwrote:

|
| Hendrik van Rooyen wrote:
| [snip]
| What do you guys think?
|
| The subject said it all. You should find some other way of entertaining
| yourself on the weekends :-)

This is the right answer...

*grin* - well - at least you *were* warned... - Hendrik

Aug 19 '06 #4
Hendrik van Rooyen wrote:
What do you guys think?
You could get something similar using an object, such as

class Hist(object):

def __init__(self):
self.vals = [None]

def __call__(self, index=-1):
return self.vals[index]

def set(self, val):
self.vals.append(val)

a = Hist()

a.set(5)
print a()

a.set('hi there')
print a()
print a(-2)

Which prints
5
hi there
5

--
Jeremy Sanders
http://www.jeremysanders.net/
Aug 21 '06 #5
You are about 7 months early.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP 8DAE142BE17999C4]
Private email: pi**@vanoostrum.org
Aug 21 '06 #6
"Piet van Oostrum" <pi**@cs.uu.nlWrote:

| You are about 7 months early.
| --

Am I? - Early for what - a seven months premature baby is small indeed...

- Hendrik

Aug 21 '06 #7
"Jeremy Sanders" <je*******************@jeremysanders.netwrote:

| Hendrik van Rooyen wrote:
|
| What do you guys think?
|
| You could get something similar using an object, such as
|
| class Hist(object):
|
| def __init__(self):
| self.vals = [None]
|
| def __call__(self, index=-1):
| return self.vals[index]
|
| def set(self, val):
| self.vals.append(val)
|
| a = Hist()
|
| a.set(5)
| print a()
|
| a.set('hi there')
| print a()
| print a(-2)
|
| Which prints
| 5
| hi there
| 5

- Sneaky! - I like this....

- Hendrik

Aug 21 '06 #8

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

Similar topics

70
by: Prashanth Ellina | last post by:
Hi, I have a feeling that OOP can be done in C also. I have used a structure to hold member variables and function pointers. The structure is used as a class to create new 'objects'. But I hit a...
14
by: Denny | last post by:
For most of my variable names, I use Hungarian notation to determine between one and the other. But what names can I use for public and private variables? I was using prv_varName and pub_varName...
19
by: | last post by:
Just seeking some advice (or solace)... Am I the only who's having trouble letting go of notation? Having extensive experience in C++ years ago (both before I jumped into VB3 in college and at...
14
by: Jeff Jarrell | last post by:
Hungarion Notation is out but.. I am trying to keep the notion of naming things "it is what it is, name it that" Class Names are an interesting side. If we name them in a meaningful way and...
66
by: CMM | last post by:
So after three years of working in .NET and stubbornly holding on to my old hungarian notation practices--- I resolved to try to rid myself of the habit. Man, I gotta say that it is liberating!!! I...
24
by: Ronald S. Cook | last post by:
An ongoing philosophical argument, I would like your opinions. With the release of .NET, Microsoft spoke of moving away from the notation as a best practice. I'm a believer for a few reasons: ...
74
by: lovecreatesbeauty | last post by:
My small function works, but I have some questions. And I want to listen to you on How it is implemented? 1. The function does not check if parameter x is larger or smaller than parameter y. ...
6
by: Grey Squirrel | last post by:
On wednesday my company will have an open ended discussion whether to standardize hungarian notation or pascal/cammel case notation. We'd love to recieve some feedback on what other people are...
12
by: inhahe | last post by:
Does anybody know of a list for canonical prefixes to use for hungarian notation in Python? Not that I plan to name all my variables with hungarian notation, but just for when it's appropriate.
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
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
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...

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.