I have a module that defines a variable with a constant value and now I
need to make that value dynamic, without affecting module clients. In
other words, I need to call a function witout using parenthesis.
Example:
mymod.py----------------------
def value():
return "hi"
client.py--------------------------
import mymod
print mymod.value
Is there a way to do this ?
Thanks. 7 2182
On Mon, 2006-02-06 at 13:54, ga************* ***@gmail.com wrote: I have a module that defines a variable with a constant value and now I need to make that value dynamic, without affecting module clients. In other words, I need to call a function witout using parenthesis. Example:
mymod.py----------------------
def value(): return "hi"
client.py-------------------------- import mymod
print mymod.value
Is there a way to do this ?
If you're not passing any arguments to this "function call", what is
this "dynamic" value supposed to depend on? The phase of the moon?
-Carsten
There are modules (like os) that define some stuff that depends on the
platform (for example linesep). This platform dependent constants gets
their values assigned when the module gets loaded, but in our
application, different threads might run on different computers (by
proxying syscalls).. and we need to calculate that value every time the
constant gets referenced. We have a slightly modified version of
python's distro to accomplish this.
It sort of a design problem, like not exposing member variables of a
class to avoid breaking clients, and use an accesor function instead.
So.. it doesn't depend on the phase of the moon.. it depends on the
architecture of a host.
I can write ten million examples of functions that doesn't depend on
arguments to calculate a dynamic value, but since you couldn't figure
that out by yourself.. I'm not going to waste my time with more
examples coz probably you won't understand them either. ga************* ***@gmail.com wrote: I have a module that defines a variable with a constant value and now I need to make that value dynamic, without affecting module clients. In other words, I need to call a function witout using parenthesis. Example:
mymod.py----------------------
def value(): return "hi"
client.py-------------------------- import mymod
print mymod.value
Is there a way to do this ?
It can be done, but you're not really supposed to be doing this, so it's
moderately horrible to do so. If you can manage, I'd avoid doing this,
but if you have to, the strategy is to (1) move the contents of your
module into a class, (2) use properties to convert what is now a method
call into an attribute access and (3) replace the module in sys.modules
with an instance of this class. This occasionally get you into trouble,
for instance reload no longer works, but it has mostly worked in my
limited experience.
Since I'm sure that's pretty opaque, here's an example:
# file noparens.py
import sys
class Module(object):
def get_value(self) :
return "hi"
value = property(get_va lue)
sys.modules[__name__] = Module()
# interpreter session import noparens noparens.value
'hi'
regards,
-tim
On Mon, 2006-02-06 at 14:19, ga************* ***@gmail.com wrote: I'm not going to waste my time with more examples coz probably you won't understand them either.
Fine, I won't waste my time trying to help you.
-Carsten.
Your post didn't provide any help at all, it was a useless sarcastic
post and I'm a very sensible person.
On Mon, 2006-02-06 at 14:38, ga************* ***@gmail.com wrote: Your post didn't provide any help at all, it was a useless sarcastic post and I'm a very sensible person.
Your original question didn't provide enough detail to offer an answer,
which is why I asked the question what the dynamic return value should
depend on. I am sorry if my conjecture that the return value might
depend on the phase of the moon offended you, but my post was not meant
to be useless. It was a necessary follow-up question because you didn't
define your requirements clearly enough.
If the condition upon which the return value is determined can be
evaluated at import time and doesn't change henceforth, a simple if
statement in the module will serve your need.
If the value *really* needs to be re-evaluated every time the name is
looked up, you'll need to follow the advice that Tim Hochberg has
already given on this thread. Note, though, that that won't prevent the
user code from caching the return value and circumventing the
re-evaluation.
-Carsten
Tim and Carsten,
Thank you very much for your replies. I'm afraid this is not going to
work for me (but I'm not 100% sure), coz if I access those modules from
the Python's C API (PyModule_* functions), the PyModule_Check( ) calls
will fail.
Thanks again. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Peter Åstrand |
last post by:
There's a new PEP available:
PEP 324: popen5 - New POSIX process module
A copy is included below. Comments are appreciated.
----
PEP: 324
Title: popen5 - New POSIX process module
|
by: dody suria wijaya |
last post by:
I found this problem when trying to split a module into two.
Here's an example:
==============
#Module a (a.py):
from b import *
class Main: pass
==============
==============
|
by: K.Simon |
last post by:
Hello,
it's very often neccessary to replace strings or a single character in
my stylesheets. My solution looks awful and very long. Now i thought
to solve this with an array like structure but i found no solution.
The original code looks like the following:
<!-- The replacement-function that i'm calling -->
<xsl:template name="replace-string">
|
by: higabe |
last post by:
Three questions
1)
I have a string function that works perfectly but according to W3C.org
web site is syntactically flawed because it contains the characters </
in sequence. So how am I supposed to write this function?
String.replace(/</g,'<');
|
by: Peter van Schie |
last post by:
Hi all,
Give an xml document that looks something like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl"
href="http://localhost/archiefassistent/xsl/fulldoc.xsl"?>
<result>
<currentpage>1</currentpage>
<document id="5">
| |
by: Lumpierbritches |
last post by:
I have code that is currently placed in the OnClose Event, and it's duplicated
in multiple forms, I would like to convert it to a module and call that module
instead of copy and pasting the code. Here is the code:
If IsNull(Forms!frm1!FormHolder) Then
DoCmd.OpenForm "Switchboard"
Else
Dim stDocName As String
Dim stLinkCriteria As String
|
by: Hvid Hat |
last post by:
Hi
I want to highlight (make it bold) a word in some text I'm getting in XML
format. My plan was to replace the word with a bold (or span) tag with the
word within the tag. I've found the code below and it works fine as long
as I'm not adding tags around the to parameter. Can anyone explain to me
why it doesn't work with tags? And it needs to be XSLT 1.0.
This works: X<xsl:value-of select="'little steak'"/>X
This doesn't work:...
|
by: Mitko Haralanov |
last post by:
I have a Python module that I have written using the C API and I am
having a problem accessing a dictionary from that module.
Here is what I have done:
1. In my init function I call
module = Py_InitModule ("name", methods);
2. then I get the module's __dict__ structure:
dict = PyModule_GetDict (module);
3. Later, I insert a dictionary that I have constructed using the
PyDict_* API
|
by: Jeff Dyke |
last post by:
I've come across an error that i'm not yet able to create a test case
for but wanted to get see if someone could shed light on this.
I have imported a module at the top of my file with
import mymodulename
this module is used many times in the current file successfully, but
then I attempt to use it one more time and get: UnboundLocalError:
local variable 'mymodulename' referenced before assignment
|
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...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |