473,804 Members | 2,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"once" assigment in Python

Hello,

I've been using Python for some DES simulations because we don't need
full C speed and it's so much faster for writing models. During
coding I find it handy to assign a variable *unless it has been
already assigned*: I've found that this is often referred to as "once"
assigment.

The best I could come up with in Python is:

try:
variable
except NameError:
variable = method()

I wonder if sombody has a solution (trick, whatever ...) which looks
more compact in coding. Something like:

once(variable, method)

doesn't work, but it would be perfect. Of course I can preprocess the
Python code but an all-Python solution would be more handy.

Any suggestions?

Thx in advance,
Lorenzo

Sep 14 '07
10 1513
Lorenzo Di Gregorio wrote:
On 17 Sep., 16:54, Larry Bates <larry.ba...@we bsafe.comwrote:
>IMHO variables like what you describe are really data not program variables.
You might consider putting variables like these in a dictionary and then check
to see if the keys exist before assignment:

var_dict={}

#
# See if 'varname' initialized, if not it needs to be
#
if 'varname' not in var_dict:
var_dict[varname]=somevalue

This is a good point!

I could write something like:

instantiate('co mponent',functi on)

and have instantiate() to conditionally do:

instance['component']=Module(functio n)

Of course this means using:

instance['component'].method()

instead of just:

component.metho d()
Well, you could do that. Or you could define a class, with a __getattr__
that returns the required default value in the case where no setting had
been made.

Then you can assign components (with their own hierarchies) to the
attributes of your top-level design ...
A more annoying difficulty is with the fact that, while the scope of
ordinary variables is limited (function, package), the scope of
'component' would be global (the whole instance[]): having global
names could be pretty annoying in modeling a hierarchy. Anyway, some
scoping mechanism could be implemented over the global dictionary and
this could be a good price to pay to avoid other tricks.
Further suggestions?
It's not actually that hard to create objects that allow you to access
their attributes using *either* attribute or indexing notation.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline

Sep 18 '07 #11

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

Similar topics

2
1407
by: dam_fool_2003 | last post by:
Friends, I come across a resent thread describes the differences between getc() vs fgetc(). There some body stated the sentence "evaluate its parameter more than once". I cannot understand the word ‘evaluate' in the message. The poster also gives a Example for the sentence, which is hard for me to understand. I searched the FAQ and I think I have missed it. The thread's link is:
7
1901
by: Petr Jakes | last post by:
I would like to do "some action" once a minute. My code (below) works, I just wonder if there is some more pythonic approach or some "trick" how to do it differently. minutes=time.localtime() while 1: min, sec = time.localtime() if sec==0 and minutes!=min: # first occur of sec==0 only!! polling 10x a second minutes=min
187
6563
by: Lasse Espeholt | last post by:
Hi... I am relativ new to the impressive and powerfull C language, but i thinks it is obsolete... The idea with header/source files where methods can clash into eachother i don't like... Look at C# which is much cleaner with namespaces. Why has C not namespaces and a "area idea" where some methods and
1
1938
by: anupam | last post by:
Can anybody please tell me what is #pragma once and why compiler gives "lvalue requirement" errors :confused:
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
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
10354
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
10359
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
6870
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
5536
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
4314
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
3837
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3005
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.