Set run vars with each call

G

Gnarlodious

Question. Is there a special method or easy way to set default values
with each call to an instance? Any ideas to make it easier? What I
want to do is have a constantly updating set of values which can be
overridden. Just thought there was an easy way to set that up.

-- Gnarlie
 
A

Alister Ware

Question. Is there a special method or easy way to set default values
with each call to an instance? Any ideas to make it easier? What I want
to do is have a constantly updating set of values which can be
overridden. Just thought there was an easy way to set that up.

-- Gnarlie

I thought that was the role of the __init__ function

class Something:
def __init__(self):
self.value="some value"
 
G

Gnarlodious

I thought that was the role of the __init__ function

class Something:
        def __init__(self):
                self.value="some value"


OK, that sets a value at init time. But is there a similar built-in to
run whenever the class instance is called?

-- Gnarlie
 
A

Andrew Berg

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

OK, that sets a value at init time. But is there a similar built-in
to run whenever the class instance is called?
What do you mean by call an instance? Do you want to run certain code
whenever any method is called? Do you want to want certain code to run
whenever an attribute is accessed? Calling an instance doesn't make any
sense, especially if you're not referring to the __init__() method.

- --
CPython 3.2.1 | Windows NT 6.1.7601.17592 | Thunderbird 5.0
PGP/GPG Public Key ID: 0xF88E034060A78FCB
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAwAGBQJOHImDAAoJEPiOA0Bgp4/LEzYH/1R+cXobysF02GiB45LcXjHm
oQsSkrbXtlvutgOlVHOI8MRVRzMndgK+0jWsujYD4nZYf45GO3b0hw/zb9jy3bUI
7BafZMRAz+wI1BJFlDD3P+IjPDoW4WvpMP0q09H4f664DYwQNuXfeveNOwAQnPXl
SpqpcvnTm0fqocC0o2G9jUuV50QXFFPntz/VVwl+3UpJLS95pCuAq+URs4OVhLM2
QB1ulmZ35PyfArdz5pYvoXvtfeURldfZhhAm1/mkVThzffUxAcCTANg6AeYd2JNb
QO0jSCedhrzWfsK5J63Ax+nrmjvms8+gZ3TxNkdMaz0zICtDuq5lLSIml1JuUfk=
=EMT2
-----END PGP SIGNATURE-----
 
I

Ian Kelly

What do you mean by call an instance? Do you want to run certain code
whenever any method is called? Do you want to want certain code to run
whenever an attribute is accessed? Calling an instance doesn't make any
sense, especially if you're not referring to the __init__() method.

If I'm understanding correctly, I think the OP wants to do something like this:

class Gadget:
def do_something(self, some_argument=some_default_value):
# do stuff

where the exact default value of some_argument depends on the current
state of the Gadget instance. The canonical approach here would be:

class Gadget:
def do_something(self, some_argument=None):
if some_argument is None:
some_argument = self._some_argument_default
# do stuff

And then the other instance methods of Gadget can update the default
by setting the value of the _some_argument_default attribute.
 
S

Steven D'Aprano

Gnarlodious said:
Question. Is there a special method or easy way to set default values
with each call to an instance? Any ideas to make it easier? What I
want to do is have a constantly updating set of values which can be
overridden. Just thought there was an easy way to set that up.

All the words are in English, but the sentences make no sense :)

Seriously, I don't understand what you mean. "Call to an instance"? Do mean
treating instances as a callable (like a function), or do you mean calling
an arbitrary method?

To make an instance itself callable, define a __call__ method.

What do you mean, "constantly updating set of values that can be
overridden"? Perhaps a simple example might help.

The closest thing I can think of, might be: you want to store a data
attribute in an instance, and use that if the caller doesn't specify
differently. Something like:

class Parrot:
name = "Polly"
def speak(self, name=None):
if name is None:
name = self.name
print("%s wants a cracker!" % name)

And in use:
Penelope wants a cracker!



If None is a legitimate value, then you can define your own sentinel to use
instead:

MISSING = object() # Unique object guaranteed not to be used by the caller.
# (Guarantee void on planet Earth.)

then replace None by MISSING in the code above.

Is this the sort of scenario you are talking about? If not, I'm completely
lost.
 
G

Gnarlodious

All the words are in English, but the sentences make no sense :)
LOL, impressive powers of mind-reading! Exactly what I needed:

import time
class Event:
epoch=time.time()
def doSomething(self, epoch=None):
if epoch is None:
epoch = self.epoch
print(epoch)

e = Event()
e.doSomething()
e.doSomething(123456789)
e.epoch = 1310522110.404471
e.doSomething()

Thanks for the help!

-- Gnarlie
http://Gnarlodious.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top