FAQ
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

Search Discussions

  • Alister Ware at Jul 12, 2011 at 2:46 pm

    On Tue, 12 Jul 2011 06:32:32 -0700, Gnarlodious wrote:

    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"




    --
    No matter how subtle the wizard, a knife in the shoulder blades will
    seriously
    cramp his style.
  • Gnarlodious at Jul 12, 2011 at 5:32 pm

    On Jul 12, 8:46?am, Alister Ware wrote:

    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
  • Andrew Berg at Jul 12, 2011 at 5:50 pm

    On 2011.07.12 12:32 PM, Gnarlodious wrote:
    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
  • Ian Kelly at Jul 12, 2011 at 6:59 pm

    On Tue, Jul 12, 2011 at 11:50 AM, Andrew Berg wrote:
    On 2011.07.12 12:32 PM, Gnarlodious wrote:
    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.
    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.
  • Ben Finney at Jul 12, 2011 at 9:36 pm

    Gnarlodious <gnarlodious at gmail.com> writes:

    OK, [the ?__init__? method] sets a value at init time. But is there a
    similar built-in to run whenever the class instance is called?
    You can write a ?__call__? method which will be called when the instance
    is called.

    But I suspect that's still not what you're asking.

    Maybe it will be quicker to ask: What is it you want to achieve?

    --
    \ ?Compulsory unification of opinion achieves only the unanimity |
    `\ of the graveyard.? ?Justice Roberts in 319 U.S. 624 (1943) |
    _o__) |
    Ben Finney
  • Steven D'Aprano at Jul 13, 2011 at 12:44 am

    Gnarlodious wrote:

    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:
    p = Parrot()
    p.speak()
    Polly wants a cracker!
    p.speak("Peter")
    Peter wants a cracker!
    p.name = "Penelope"
    p.speak()
    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.


    --
    Steven
  • Gnarlodious at Jul 13, 2011 at 2:01 am

    On Jul 12, 6:44?pm, Steven D'Aprano wrote:

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 12, '11 at 1:32p
activeJul 13, '11 at 2:01a
posts8
users6
websitepython.org

People

Translate

site design / logo © 2022 Grokbase