Re: isinstance()

2023-08-04 Thread Grant Edwards via Python-list
On 2023-08-04, Chris Angelico via Python-list wrote: > On Sat, 5 Aug 2023 at 09:36, dn via Python-list > wrote: > >> Faced with a situation where an argument may be a scalar-value or an >> iterable, I'll presume the latter, eg throw it straight into a for-loop. >> If that fails (because the argu

Re: isinstance()

2023-08-04 Thread Chris Angelico via Python-list
On Sat, 5 Aug 2023 at 09:36, dn via Python-list wrote: > Faced with a situation where an argument may be a scalar-value or an > iterable, I'll presume the latter, eg throw it straight into a for-loop. > If that fails (because the argument is a scalar), use try-except to > re-route the logic. That

Re: isinstance()

2023-08-04 Thread dn via Python-list
On 05/08/2023 11.18, Chris Angelico via Python-list wrote: On Sat, 5 Aug 2023 at 09:08, dn via Python-list wrote: On 03/08/2023 11.38, Jon Ribbens via Python-list wrote: On 2023-08-02, dn wrote: Can you please explain why a multi-part second-argument must be a tuple and not any other form o

Re: isinstance()

2023-08-04 Thread Chris Angelico via Python-list
On Sat, 5 Aug 2023 at 09:08, dn via Python-list wrote: > > On 03/08/2023 11.38, Jon Ribbens via Python-list wrote: > > On 2023-08-02, dn wrote: > >> Can you please explain why a multi-part second-argument must be a tuple > >> and not any other form of collection-type? > > > > The following commen

Re: isinstance()

2023-08-04 Thread dn via Python-list
On 03/08/2023 11.38, Jon Ribbens via Python-list wrote: On 2023-08-02, dn wrote: Can you please explain why a multi-part second-argument must be a tuple and not any other form of collection-type? The following comment may hold a clue: if (PyTuple_Check(cls)) { /* Not a general

Re: isinstance()

2023-08-04 Thread Jon Ribbens via Python-list
On 2023-08-02, dn wrote: > Can you please explain why a multi-part second-argument must be a tuple > and not any other form of collection-type? The following comment may hold a clue: if (PyTuple_Check(cls)) { /* Not a general sequence -- that opens up the road to recursio

Re: isinstance()

2023-08-02 Thread Cameron Simpson via Python-list
On 03Aug2023 10:14, dn wrote: Can you please explain why a multi-part second-argument must be a tuple and not any other form of collection-type? The signature is: isinstance(object, classinfo) leading to "classinfo" of: 1/ a single class/type, eg int 2/ a tuple of same, eg ( int, str, ) 3/ a

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Chris Angelico
On Fri, Aug 28, 2015 at 1:02 PM, Steven D'Aprano wrote: > On Fri, 28 Aug 2015 12:33 am, Chris Angelico wrote: > >> On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano >> wrote: >>> On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote: >>> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico wro

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Steven D'Aprano
On Fri, 28 Aug 2015 12:33 am, Chris Angelico wrote: > On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano > wrote: >> On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote: >> >>> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico >>> wrote: Or is there a magic __isinstance__ >>> >>> Argh, keyed th

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Chris Angelico
On Thu, Aug 27, 2015 at 10:59 PM, Steven D'Aprano wrote: > On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote: > >> On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico wrote: >>> Or is there a magic __isinstance__ >> >> Argh, keyed the wrong thing and sent the post prematurely. Meant to say: >> >> Or

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Steven D'Aprano
On Thu, 27 Aug 2015 09:14 am, Chris Angelico wrote: > On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico wrote: >> Or is there a magic __isinstance__ > > Argh, keyed the wrong thing and sent the post prematurely. Meant to say: > > Or is there a magic __instancecheck__ method somewhere that I'm not

Re: isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Rob Gaddi
On Thu, 27 Aug 2015 09:12:55 +1000, Chris Angelico wrote: > > You're omitting some context here, but after poking around with your > module a bit, I'm guessing you created bf somewhat thus? > bf = bitfield.make_bf("bf", (("asdf",bitfield.c_uint,2),)) bf > bf.__mro__ > (, , '_ctyp

Re: isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Chris Angelico
On Thu, Aug 27, 2015 at 9:12 AM, Chris Angelico wrote: > Or is there a magic __isinstance__ Argh, keyed the wrong thing and sent the post prematurely. Meant to say: Or is there a magic __instancecheck__ method somewhere that I'm not aware of? ChrisA -- https://mail.python.org/mailman/listinfo/

Re: isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Chris Angelico
On Thu, Aug 27, 2015 at 7:21 AM, Rob Gaddi wrote: > I'm running into some strangeness trying to work with the bitfield module > from my ctypes-bitfield package (on PyPi). I'm trying to use isinstance > (), and it's kinda sorta lying to me. > > - IPython session (Python 3.4 under Linux) --

Re: isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Terry Reedy
On 8/26/2015 5:21 PM, Rob Gaddi wrote: I'm running into some strangeness trying to work with the bitfield module from my ctypes-bitfield package (on PyPi). I'm trying to use isinstance (), and it's kinda sorta lying to me. isinstace(inst, klass) is implemented as klass.__instancecheck__(inst)

Re: isinstance(.., file) for Python 3

2012-11-08 Thread Stefan Behnel
Duncan Booth, 08.11.2012 14:58: > Ulrich Eckhardt wrote: >> If possible, I'm looking for a solution that works for Pythons 2 and 3, >> since I'm not fully through the conversion yet and have clients that >> might use the older snake for some time before shedding their skin. >> >> Suggestions? >

Re: isinstance(.., file) for Python 3

2012-11-08 Thread Duncan Booth
Ulrich Eckhardt wrote: > If possible, I'm looking for a solution that works for Pythons 2 and 3, > since I'm not fully through the conversion yet and have clients that > might use the older snake for some time before shedding their skin. > > Suggestions? Why bother checking types at all? def

Re: isinstance(.., file) for Python 3

2012-11-08 Thread Steven D'Aprano
On Thu, 08 Nov 2012 13:05:22 +0100, Ulrich Eckhardt wrote: > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used "isinstance(p, file)" to > determine whether the par

Re: isinstance(.., file) for Python 3

2012-11-08 Thread Peter Otten
Ulrich Eckhardt wrote: > Hi! > > I have two problems that are related and that I'd like to solve together. > > Firstly, I have code that allows either a file or a string representing > its content as parameter. If the parameter is a file, the content is > read from the file. In Python 2, I used

Re: isinstance(.., file) for Python 3

2012-11-08 Thread Chris Angelico
On Thu, Nov 8, 2012 at 11:05 PM, Ulrich Eckhardt wrote: > Firstly, I have code that allows either a file or a string representing its > content as parameter. If the parameter is a file, the content is read from > the file. In Python 2, I used "isinstance(p, file)" to determine whether the > parame

Re: isinstance(.., file) for Python 3

2012-11-08 Thread MRAB
On 2012-11-08 12:05, Ulrich Eckhardt wrote: Hi! I have two problems that are related and that I'd like to solve together. Firstly, I have code that allows either a file or a string representing its content as parameter. If the parameter is a file, the content is read from the file. In Python 2,

Re: "isinstance" question

2010-06-23 Thread Thomas Jollans
On 06/23/2010 08:39 AM, Satish Eerpini wrote: > > > I want to test whether an object is an instance of any > user-defined > class. "isinstance" is less helpful than one would expect. > > >>> import types > >>> class foo() : # define dummy class >

Re: "isinstance" question

2010-06-22 Thread Satish Eerpini
> >I want to test whether an object is an instance of any user-defined >> class. "isinstance" is less helpful than one would expect. >> >> >>> import types >> >>> class foo() : # define dummy class >> ... pass >> ... >> >>> x = foo() >> >>> >> >>> type(x) >> >> >>> >> >>> isinstanc

Re: "isinstance" question

2010-06-22 Thread Gabriel Genellina
En Tue, 22 Jun 2010 23:45:07 -0300, John Nagle escribió: I want to test whether an object is an instance of any user-defined class. "isinstance" is less helpful than one would expect. >>> import types >>> class foo() : # define dummy class ... pass ... >>> x = foo() >>> >>> type

Re: "isinstance" question

2010-06-22 Thread John Nagle
On 6/22/2010 8:13 PM, Ben Finney wrote: John Nagle writes: I want to test whether an object is an instance of any user-defined class. "isinstance" is less helpful than one would expect. Right. The type hierarchy is now unified; there's essentially no difference in later Python versions b

Re: "isinstance" question

2010-06-22 Thread Ben Finney
John Nagle writes: > I want to test whether an object is an instance of any user-defined > class. "isinstance" is less helpful than one would expect. Right. The type hierarchy is now unified; there's essentially no difference in later Python versions between user-defined types and built-in ty

Re: isinstance(False, int)

2010-03-08 Thread Bruno Desthuilliers
Rolando Espinoza La Fuente a écrit : On Fri, Mar 5, 2010 at 2:32 PM, mk wrote: Arnaud Delobelle wrote: 1 == True True 0 == False True So what's your question? Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl, but not in Python.. Although I can understand the ra

Re: isinstance(False, int)

2010-03-05 Thread Robert Kern
On 2010-03-05 17:48 PM, Jack Diederich wrote: On Fri, Mar 5, 2010 at 6:09 PM, Steven D'Aprano wrote: On Fri, 05 Mar 2010 15:58:01 -0500, Jack Diederich wrote: So, the pythonic way to check for True/False should be: 1 is True False Why do you need to check for True/False? You should n

Re: isinstance(False, int)

2010-03-05 Thread Chris Rebert
On Fri, Mar 5, 2010 at 1:51 PM, Terry Reedy wrote: > On 3/5/2010 1:54 PM, Jean-Michel Pichavant wrote: >> Steven D'Aprano wrote: > >> Despite there are good reasons for bool to be int, the newcomer 'wtf' >> reaction at first glance is legitimate. >> Starting python from scratch, booleans would hav

Re: isinstance(False, int)

2010-03-05 Thread Jack Diederich
On Fri, Mar 5, 2010 at 6:09 PM, Steven D'Aprano wrote: > On Fri, 05 Mar 2010 15:58:01 -0500, Jack Diederich wrote: > So, the pythonic way to check for True/False should be: >>> 1 is True False >>> >>> Why do you need to check for True/False? >>> >>> >> You should never check for

Re: isinstance(False, int)

2010-03-05 Thread Steven D'Aprano
On Fri, 05 Mar 2010 15:58:01 -0500, Jack Diederich wrote: >>> So, the pythonic way to check for True/False should be: >>> >> 1 is True >>> False >> >> Why do you need to check for True/False? >> >> > You should never check for "is" False/True but always check for > equality. The reason is tha

Re: isinstance(False, int)

2010-03-05 Thread Robert Kern
On 2010-03-05 14:58 PM, Jack Diederich wrote: On Fri, Mar 5, 2010 at 2:54 PM, Steven D'Aprano wrote: On Fri, 05 Mar 2010 15:01:23 -0400, Rolando Espinoza La Fuente wrote: On Fri, Mar 5, 2010 at 2:32 PM, mk wrote: Arnaud Delobelle wrote: 1 == True True 0 == False True So what's you

Re: isinstance(False, int)

2010-03-05 Thread Terry Reedy
On 3/5/2010 1:54 PM, Jean-Michel Pichavant wrote: Steven D'Aprano wrote: Despite there are good reasons for bool to be int, the newcomer 'wtf' reaction at first glance is legitimate. Starting python from scratch, booleans would have not been a subclass of int (just guessing though), 'cause it

Re: isinstance(False, int)

2010-03-05 Thread Terry Reedy
On 3/5/2010 1:30 PM, MRAB wrote: mk wrote: >>> isinstance(False, int) True >>> >>> isinstance(True, int) True Huh? >>> >>> issubclass(bool, int) True Huh?! Python didn't have Booleans originally, 0 and 1 were used instead. When bool was introduced it was made a subclass of int so that exist

Re: isinstance(False, int)

2010-03-05 Thread Jack Diederich
On Fri, Mar 5, 2010 at 2:54 PM, Steven D'Aprano wrote: > On Fri, 05 Mar 2010 15:01:23 -0400, Rolando Espinoza La Fuente wrote: > >> On Fri, Mar 5, 2010 at 2:32 PM, mk wrote: >>> Arnaud Delobelle wrote: >>> >>> 1 == True True >>> >>> 0 == False True So wha

Re: isinstance(False, int)

2010-03-05 Thread Steven D'Aprano
On Fri, 05 Mar 2010 15:01:23 -0400, Rolando Espinoza La Fuente wrote: > On Fri, Mar 5, 2010 at 2:32 PM, mk wrote: >> Arnaud Delobelle wrote: >> >> 1 == True >>> >>> True >> >> 0 == False >>> >>> True >>> >>> So what's your question? >> >> Well nothing I'm just kind of bewildered: I'd

Re: isinstance(False, int)

2010-03-05 Thread mk
Rolando Espinoza La Fuente wrote: Doesn't have side effects not knowing that False/True are ints? It does, in fact I was wondering why my iterator didn't work until I figured issubclass(bool, int) is true. Regards, mk -- http://mail.python.org/mailman/listinfo/python-list

Re: isinstance(False, int)

2010-03-05 Thread Rolando Espinoza La Fuente
On Fri, Mar 5, 2010 at 2:32 PM, mk wrote: > Arnaud Delobelle wrote: > > 1 == True >> >> True > > 0 == False >> >> True >> >> So what's your question? > > Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl, > but not in Python.. Although I can understand the rat

Re: isinstance(False, int)

2010-03-05 Thread Jean-Michel Pichavant
Steven D'Aprano wrote: On Fri, 05 Mar 2010 18:14:16 +0100, mk wrote: isinstance(False, int) True >>> >>> isinstance(True, int) True Huh? Yes. Do you have an actual question? >>> issubclass(bool, int) True Huh?! Exactly. Bools are a late-comer to Python.

Re: isinstance(False, int)

2010-03-05 Thread mk
Arnaud Delobelle wrote: 1 == True True 0 == False True So what's your question? Well nothing I'm just kind of bewildered: I'd expect smth like that in Perl, but not in Python.. Although I can understand the rationale after skimming PEP 285, I still don't like it very much. Regards, mk

Re: isinstance(False, int)

2010-03-05 Thread MRAB
mk wrote: >>> isinstance(False, int) True >>> >>> isinstance(True, int) True Huh? >>> >>> issubclass(bool, int) True Huh?! Python didn't have Booleans originally, 0 and 1 were used instead. When bool was introduced it was made a subclass of int so that existing code wouldn't break. -- h

Re: isinstance(False, int)

2010-03-05 Thread Rolando Espinoza La Fuente
On Fri, Mar 5, 2010 at 2:00 PM, Steve Holden wrote: [...] > > Just a brainfart from the BDFL - he decided (around 2.2.3, IIRC) that it > would be a good ideal for Booleans to be a subclass of integers. > I would never figured out >>> bool.__bases__ (,) Doesn't have side effects not knowing tha

Re: isinstance(False, int)

2010-03-05 Thread Steven D'Aprano
On Fri, 05 Mar 2010 18:14:16 +0100, mk wrote: isinstance(False, int) > True > >>> > >>> isinstance(True, int) > True > > Huh? Yes. Do you have an actual question? > >>> issubclass(bool, int) > True > > Huh?! Exactly. Bools are a late-comer to Python. For historical and implementatio

Re: isinstance(False, int)

2010-03-05 Thread Stephen Hansen
On Fri, Mar 5, 2010 at 9:14 AM, mk wrote: > >>> isinstance(False, int) > True > >>> > >>> isinstance(True, int) > True > > Huh? > > >>> > >>> issubclass(bool, int) > True > > Huh?! > Huh, what? http://www.python.org/dev/peps/pep-0285/ --S -- http://mail.python.org/mailman/listinfo/python-list

Re: isinstance(False, int)

2010-03-05 Thread Arnaud Delobelle
mk writes: isinstance(False, int) > True isinstance(True, int) > True > > Huh? > issubclass(bool, int) > True > > Huh?! > > Regards, > mk Yes, and: >>> True + False 1 In fact: >>> 1 == True True >>> 0 == False True So what's your question? -- Arnaud -- http://mail

Re: isinstance(False, int)

2010-03-05 Thread Steve Holden
mk wrote: isinstance(False, int) > True isinstance(True, int) > True > > Huh? > issubclass(bool, int) > True > > Huh?! > >>> 3+True 4 >>> 3+False 3 >>> Just a brainfart from the BDFL - he decided (around 2.2.3, IIRC) that it would be a good ideal for Booleans to be a s

Re: isinstance(obj, type(obj)) == True?

2009-06-24 Thread Terry Reedy
Art wrote: I have the following problem: ipdb> p type(self) ipdb> isinstance(self, component.BiasComponent) False I thought that isinstance(obj, type(obj)) == True. Yes, but that is not what you entered ;-). The name 'component.BiasComponent' is not bound to type(self), but to another objec

Re: isinstance(obj, type(obj)) == True?

2009-06-24 Thread Chris Rebert
On Wed, Jun 24, 2009 at 7:57 AM, Art wrote: > I have the following problem: > > ipdb> p type(self) > > > ipdb> isinstance(self, component.BiasComponent) > False > > I thought that isinstance(obj, type(obj)) == True. > > The specific problem is when I try to call the super of a class and it > only