[EMAIL PROTECTED]: Re: overriding methods - two questions]

2007-11-19 Thread J. Clifford Dyer
On Mon, Nov 19, 2007 at 04:33:39PM +0100, Bruno Desthuilliers wrote regarding Re: overriding methods - two questions: > > J. Clifford Dyer a ?crit : > > On Mon, Nov 19, 2007 at 01:41:46PM +0100, Bruno Desthuilliers wrote > > regarding Re: overriding methods - two questions: &g

Re: overriding methods - two questions

2007-11-19 Thread Bruno Desthuilliers
J. Clifford Dyer a écrit : > On Mon, Nov 19, 2007 at 01:41:46PM +0100, Bruno Desthuilliers wrote regarding > Re: overriding methods - two questions: >> Steven D'Aprano a ?crit : >>> On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: >>> >>&

Re: overriding methods - two questions

2007-11-19 Thread George Sakkis
On Nov 19, 7:44 am, Bruno Desthuilliers wrote: > George Sakkis a écrit : > > > > > On Nov 16, 5:03 pm, Steven D'Aprano <[EMAIL PROTECTED] > > cybersource.com.au> wrote: > > >> On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: > Question 1: > Given that the user of the API ca

Re: overriding methods - two questions

2007-11-19 Thread J. Clifford Dyer
On Mon, Nov 19, 2007 at 01:41:46PM +0100, Bruno Desthuilliers wrote regarding Re: overriding methods - two questions: > > Steven D'Aprano a ?crit : > > On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: > > > >>> Question 1: > >>> >

Re: overriding methods - two questions

2007-11-19 Thread Bruno Desthuilliers
George Sakkis a écrit : > On Nov 16, 5:03 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: > >> On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: Question 1: Given that the user of the API can choose to override foo() or not, how can I control the sig

Re: overriding methods - two questions

2007-11-19 Thread Bruno Desthuilliers
Steven D'Aprano a écrit : > On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: > >>> Question 1: >>> >>> Given that the user of the API can choose to override foo() or not, how >>> can I control the signature that they use? >> While technically possible (using inspect.getargspec), tryi

Re: overriding methods - two questions

2007-11-17 Thread Steven D'Aprano
On Fri, 16 Nov 2007 18:30:28 -0800, George Sakkis wrote: > On Nov 16, 5:03 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: > >> On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: >> >> Question 1: >> >> >> Given that the user of the API can choose to override foo()

Re: overriding methods - two questions

2007-11-17 Thread Steven D'Aprano
On Sat, 17 Nov 2007 12:19:07 +0200, Donn Ingle wrote: >> BTW, it is a convention for method names to be lower case, and classes >> to be Title case. Seeing something like obj.Draw, most(?) Python >> developers will expect that the Draw attribute of obj is itself a >> class: > Thanks, I'm pretty da

Re: overriding methods - two questions

2007-11-17 Thread Donn Ingle
Thanks, good tips all-round. I have it working okay at the moment with all the suggestions. It may break in future, but that's another day :) /d -- http://mail.python.org/mailman/listinfo/python-list

Re: overriding methods - two questions

2007-11-17 Thread Donn Ingle
> *not* being called by the user but *by* my API (in a timeout loop). > > You don't know that. How can you possibly guarantee that the user won't > find some other use for the draw() method Well, as per your good examples, I would answer that as the parameters passed to draw() grow in number, so

Re: overriding methods - two questions

2007-11-17 Thread Peter Otten
Donn Ingle wrote: >> I am curious as to why you want to go through such contortions.  What >> do you gain. > for obj in list: > if obj has a foo() method: > a = something > b = figureitout ( ) > object.foo ( a, b ) > > I am accepting objects of any class on a stack. Depending on their na

Re: overriding methods - two questions

2007-11-17 Thread Steven D'Aprano
On Sat, 17 Nov 2007 06:47:18 +0200, Donn Ingle wrote: >>> While technically possible (using inspect.getargspec), trying to make >>> your code idiot-proof is a lost fight and a pure waste of time. > >> Worse: it's actually counter-productive! The whole idea of being able >> to subclass a class me

Re: overriding methods - two questions

2007-11-17 Thread Gabriel Genellina
En Sat, 17 Nov 2007 01:56:22 -0300, Donn Ingle <[EMAIL PROTECTED]> escribió: > for obj in list: > if 'foo' in obj.__class__.__dict__: > etc. > > Although I am concerned that it's a loop ('in') and so may be slower than > some other way to detect foo(). 'in' for dictionaries is fast and runs

Re: overriding methods - two questions

2007-11-17 Thread Donn Ingle
> This is strictly a documentation matter, in my mind. Python does not > offer any means to enforce the calling sequence of an "override method". Yes, I hear you. > You might be able to wrap YOUR calling code with a try/except block > to trap errors if the callback doesn't like the "documented AP

Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
> I am curious as to why you want to go through such contortions.  What > do you gain. for obj in list: if obj has a foo() method: a = something b = figureitout ( ) object.foo ( a, b ) I am accepting objects of any class on a stack. Depending on their nature I want to call certain methods

Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
>> While technically possible (using inspect.getargspec), trying to make >> your code idiot-proof is a lost fight and a pure waste of time. > Worse: it's actually counter-productive! > The whole idea of being able to subclass a class means that the user > should be able to override foo() *includi

Re: overriding methods - two questions

2007-11-16 Thread George Sakkis
On Nov 16, 5:03 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: > >> Question 1: > > >> Given that the user of the API can choose to override foo() or not, how > >> can I control the signature that they use? > > > W

Re: overriding methods - two questions

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote: >> Question 1: >> >> Given that the user of the API can choose to override foo() or not, how >> can I control the signature that they use? > > While technically possible (using inspect.getargspec), trying to make > your code idiot-pr

Re: overriding methods - two questions

2007-11-16 Thread [EMAIL PROTECTED]
On Nov 16, 11:35 am, Donn Ingle <[EMAIL PROTECTED]> wrote: > >This may help (on an old Python version): > class Sam: pass > class Judy: > > ...def foo(self): pass > > ... > children = [Sam(), Judy(), Sam()] > for child in children: hasattr(child, "foo") > > ... > > False > >

Re: overriding methods - two questions

2007-11-16 Thread Bruno Desthuilliers
Donn Ingle a écrit : >> for child in self.childrens: >> if 'foo' in child.__class__.__dict__: >> child.foo() > Bruno, you're the man! I really must take the time to look into all those > under-under score things! Knowing Python's object model can help, indeed !-) Now while this kind of stuff is

Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
> for child in self.childrens: > if 'foo' in child.__class__.__dict__: > child.foo() Bruno, you're the man! I really must take the time to look into all those under-under score things! Thanks. /d -- http://mail.python.org/mailman/listinfo/python-list

Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
>This may help (on an old Python version): class Sam: pass class Judy: > ...def foo(self): pass > ... children = [Sam(), Judy(), Sam()] for child in children: hasattr(child, "foo") > ... > False > True > False That's not what my tests are showing. While Sam has no foo, it's

Re: overriding methods - two questions

2007-11-16 Thread Bruno Desthuilliers
Donn Ingle a écrit : > Hi, > Here's a framework for the questions: > > --- In a module, part of an API --- > class Basis ( object ): > def foo ( self, arg ): > pass > > --- In user's own code --- > class Child ( Basis ): > def foo ( self, not, sure ): > ... > > > Question 1: > > Given th

Re: overriding methods - two questions

2007-11-16 Thread bearophileHUGS
Donn Ingle: > Say I am in class Basis, doing a loop and I have a list of Child objects. I > want to run the foo() method for each one that *has* a foo() method. This may help (on an old Python version): >>> class Sam: pass ... >>> class Judy: ... def foo(self): pass ... >>> children = [Sam()

Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
> Actually, Python is complaining about your user's poor choice of > argument names. 'not' is a reserved keyword. My example was poor, but my actual test code did't use 'not'. Python simply checks the use of foo() to the local sig of foo() and does not go up the chain. This is understandable and

Re: overriding methods - two questions

2007-11-16 Thread Paul McGuire
On Nov 16, 11:03 am, Donn Ingle <[EMAIL PROTECTED]> wrote: > Hi, > Here's a framework for the questions: > > --- In a module, part of an API --- > class Basis ( object ): > def foo ( self, arg ): > pass > > --- In user's own code --- > class Child ( Basis ): > def foo ( self, not, sure ): > .

overriding methods - two questions

2007-11-16 Thread Donn Ingle
Hi, Here's a framework for the questions: --- In a module, part of an API --- class Basis ( object ): def foo ( self, arg ): pass --- In user's own code --- class Child ( Basis ): def foo ( self, not, sure ): ... Question 1: Given that the user of the API can choose to override foo() or