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
) will be mono-dimensional 99.9% of the time. NB having said that, the underlying mechanism *is* multi-dimensional: "direct, indirect, or virtual) subclass thereof" (https://docs.python.org/3/library/functions.html#isinstance) Further: the 'rules' say: "cl

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, s

isinstance()

2023-08-02 Thread dn via Python-list
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 union type, eg

Re: try..except or type() or isinstance()?

2020-08-16 Thread Alexa Oña
n-list@python.org Asunto: Re: try..except or type() or isinstance()? On Sun, 16 Aug 2020 09:40:12 +0200 Manfred Lotz wrote: > On Sat, 15 Aug 2020 12:20:48 -0400 > Dennis Lee Bieber wrote: > > > On Sat, 15 Aug 2020 15:31:56 +0200, Manfred Lotz > > declaimed the following: &

Re: try..except or type() or isinstance()?

2020-08-16 Thread Manfred Lotz
On Sun, 16 Aug 2020 09:40:12 +0200 Manfred Lotz wrote: > On Sat, 15 Aug 2020 12:20:48 -0400 > Dennis Lee Bieber wrote: > > > On Sat, 15 Aug 2020 15:31:56 +0200, Manfred Lotz > > declaimed the following: > > > > >On Sat, 15 Aug 2020 11:47:03 +0200 > > >Sibylle Koczian wrote: > > > > >

Re: try..except or type() or isinstance()?

2020-08-16 Thread Manfred Lotz
On 15 Aug 2020 14:49:48 GMT r...@zedat.fu-berlin.de (Stefan Ram) wrote: > Manfred Lotz writes: > >Here a minimal example > > main.py > > source=""" > sehr gut > 1 > """[ 1: -1 ].split( "\n" ) > > class grades: > names =[ "sehr gut" ] > @staticmethod > def is_numeric( text ): >

Re: try..except or type() or isinstance()?

2020-08-16 Thread Manfred Lotz
On Sat, 15 Aug 2020 12:20:48 -0400 Dennis Lee Bieber wrote: > On Sat, 15 Aug 2020 15:31:56 +0200, Manfred Lotz > declaimed the following: > > >On Sat, 15 Aug 2020 11:47:03 +0200 > >Sibylle Koczian wrote: > > > > >> if the value comes from a file, isn't it a > >> string in any case? A string

Re: try..except or type() or isinstance()?

2020-08-15 Thread Manfred Lotz
On Sat, 15 Aug 2020 11:47:03 +0200 Sibylle Koczian wrote: > Am 15.08.2020 um 10:14 schrieb Manfred Lotz: > > Hi Chris, > > Thanks a lot for you advice. > > > > On Sat, 15 Aug 2020 16:15:29 +1000 > > Chris Angelico wrote: > > > >> On Sat, Aug 15, 2020 at 3:36 PM Manfred Lotz > >> wrote: > >>

Re: try..except or type() or isinstance()?

2020-08-15 Thread Sibylle Koczian
Am 15.08.2020 um 10:14 schrieb Manfred Lotz: Hi Chris, Thanks a lot for you advice. On Sat, 15 Aug 2020 16:15:29 +1000 Chris Angelico wrote: On Sat, Aug 15, 2020 at 3:36 PM Manfred Lotz wrote: I have an object which I could initialize providind an int or a str. ... For my use case I do

Re: try..except or type() or isinstance()?

2020-08-15 Thread Manfred Lotz
. > >> > >> I am not sure which of the following is best to use > >> - try/except > >> - if type(int)... > >> - if isinstance(v, int) > >> > >> Here a minimal example > >> > >> def get_id(fromname): > >&g

Re: try..except or type() or isinstance()?

2020-08-15 Thread Manfred Lotz
f the following is best to use > > - try/except > > - if type(int)... > > - if isinstance(v, int) > > > > Here a minimal example > > > > def get_id(fromname): > > # do something with `fromname` > > return 0 > > > > def get_name

Re: try..except or type() or isinstance()?

2020-08-14 Thread Peter Otten
Chris Angelico wrote: > On Sat, Aug 15, 2020 at 3:36 PM Manfred Lotz wrote: >> >> I have an object which I could initialize providind an int or a str. >> >> I am not sure which of the following is best to use >> - try/except >> - if type(int)... >>

Re: try..except or type() or isinstance()?

2020-08-14 Thread Chris Angelico
On Sat, Aug 15, 2020 at 3:36 PM Manfred Lotz wrote: > > I have an object which I could initialize providind an int or a str. > > I am not sure which of the following is best to use > - try/except > - if type(int)... > - if isinstance(v, int) > > Here a minimal exam

try..except or type() or isinstance()?

2020-08-14 Thread Manfred Lotz
I have an object which I could initialize providind an int or a str. I am not sure which of the following is best to use - try/except - if type(int)... - if isinstance(v, int) Here a minimal example def get_id(fromname): # do something with `fromname` return 0 def get_name(fromid

Re: strings and ints consistency - isinstance

2016-09-22 Thread Steve D'Aprano
On Thu, 22 Sep 2016 11:40 pm, Sayth Renshaw wrote: > True it failed, just actually happy to get it to fail or pass successfully > on int input. But it doesn't. It raises ValueError no matter what you enter. -- Steve “Cheer up,” they said, “things could be worse.” So I cheered up, and sure e

Re: strings and ints consistency - isinstance

2016-09-22 Thread Larry Hudson via Python-list
in trying to get this to work. Half opt for try except the other half if else and then implement them largely differently. Every many and varied approach str2bool(), isalpha() using list with isinstance(var, [ int, str, bool]) etc. Anyway back to the old drawing board. Cheers Sayth IMHO

Re: strings and ints consistency - isinstance

2016-09-22 Thread Sayth Renshaw
e input() returns a string in Python 3, this will always be a string. > > > > if isinstance(int(answer), int) is True: > > int(answer) will either succeed, or it will fail. If it fails, it will raise > ValueError, and your code will fail with an exception. > > If it su

Re: strings and ints consistency - isinstance

2016-09-22 Thread Gregory Ewing
On Wednesday, September 21, 2016 at 11:41:42 PM UTC-4, Sayth Renshaw wrote: answer = input("\t >> ") if isinstance(int(answer), int) is True: raise ValueError("Ints aren't valid input") You seem to be trying to check that the user hasn't entered an in

Re: strings and ints consistency - isinstance

2016-09-22 Thread Steve D'Aprano
s will always be a string. > if isinstance(int(answer), int) is True: int(answer) will either succeed, or it will fail. If it fails, it will raise ValueError, and your code will fail with an exception. If it succeeds, then it will return an int. Testing whether int(answer) returns an int is a

Re: strings and ints consistency - isinstance

2016-09-22 Thread Ned Batchelder
On Wednesday, September 21, 2016 at 11:41:42 PM UTC-4, Sayth Renshaw wrote: > This ends being the code I can use to get it to work, seems clear and > pythonic, open to opinion on that :-) > > > answer = input("\t >> ") > if isinstance(int(answer), int) is

Re: strings and ints consistency - isinstance

2016-09-22 Thread Lawrence D’Oliveiro
On Thursday, September 22, 2016 at 3:41:42 PM UTC+12, Sayth Renshaw wrote: > if isinstance(int(answer), int) is True: Not sure what the point of this is... -- https://mail.python.org/mailman/listinfo/python-list

Re: strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
This ends being the code I can use to get it to work, seems clear and pythonic, open to opinion on that :-) answer = input("\t >> ") if isinstance(int(answer), int) is True: raise ValueError("Ints aren't valid input") sys.exit() elif isinstance(answer, str

Re: strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
none-true-or-false ___ I actually thought this would have resolved my issues and still returned error if ints entered however it still passes through. answer = input("\t >> ") if isinstance(answer, str) is True: prin

Re: strings and ints consistency - isinstance

2016-09-21 Thread Steve D'Aprano
ou can't str () and get a > valuerror you use conditional logic with strings. What sort of conditional logic do you think you will use to determine whether or not str(x) will fail? > Therefore to try and keep with pythons only one obvious way of doing > things should i prefer condi

Re: strings and ints consistency - isinstance

2016-09-21 Thread John Gordon
In Sayth Renshaw writes: > Trying to clarify why ints and strings arent treated the same. Because they are not the same. > You can get a valuerror from trying to cast a non-int to an int as in > int(3.0) however you cannot do a non string with str(a). Anything you type can be represented as

Re: strings and ints consistency - isinstance

2016-09-21 Thread Ned Batchelder
vious way of doing things > should i prefer conditional logic for all using isinstance? > > That way regardless of input type my code flows the same and more explicitly > states the intended type. > Sayth How you do your check depends a lot on why you are checking, how strange a value y

strings and ints consistency - isinstance

2016-09-21 Thread Sayth Renshaw
valuerror. However as you can't str () and get a valuerror you use conditional logic with strings. Therefore to try and keep with pythons only one obvious way of doing things should i prefer conditional logic for all using isinstance? That way regardless of input type my code flows the same and

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Chris Angelico
t;>> 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 __instan

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Steven D'Aprano
r 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? >> >> Yes -- see Terry Reedy&#x

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Chris Angelico
ent the post prematurely. Meant to say: >> >> Or is there a magic __instancecheck__ method somewhere that I'm not aware >> of? > > Yes -- see Terry Reedy's post. > > isinstance(inst, klass) ends up calling > > type(klass).__instancecheck__(klass, inst) I

Re: isinstance() and multiple inheritance/ctypes

2015-08-27 Thread Steven D'Aprano
od somewhere that I'm not aware > of? Yes -- see Terry Reedy's post. isinstance(inst, klass) ends up calling type(klass).__instancecheck__(klass, inst) -- Steven -- https://mail.python.org/mailman/listinfo/python-list

Re: isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Rob Gaddi
quot;,bitfield.c_uint,2),)) >>>> bf > >>>> bf.__mro__ > (, , '_ctypes._CData'>, , ) >>>> type(bf) > >>>> type(bf).__mro__ > (, , ) > > If that's the case, then it's quite correct: bf is not a Bitfield, it is > a su

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 s

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.__insta

isinstance() and multiple inheritance/ctypes

2015-08-26 Thread Rob Gaddi
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) --- In [649]: bf.__mro__ Out[649]

Re: TypeCheck vs IsInstance in C API

2015-08-25 Thread random832
ted right above it. It was, apparently, the recommended way to do this in python up to 1.4, before isinstance existed. It only worked with [old-style, goes without saying] instance and class objects and not, say, ints, but AFAICT isinstance in 1.5 didn't support anything else either. Since t

Re: TypeCheck vs IsInstance in C API

2015-08-25 Thread Terry Reedy
On 8/25/2015 10:32 AM, random...@fastmail.us wrote: On Mon, Aug 24, 2015, at 22:12, Andrew Wang wrote: [question snipped] And, now, your moment of zen: Huh?? I fail to see the point of this buggy code. def isa(ob, cl): try: raise ob except cl: return 1 else: return

Re: TypeCheck vs IsInstance in C API

2015-08-25 Thread random832
answer in 2006, since we didn't have either Python 3 or abstract classes back then. The IsInstance (and IsSubclass) function documentation mentions that they use the methods __instancecheck__ [and __subclasscheck__], presumably to support abstract classes. The actual implementation of

TypeCheck vs IsInstance in C API

2015-08-25 Thread Andrew Wang
Hi. I know this thread is ancient, but I would like to know the answer as well ( https://mail.python.org/pipermail/python-list/2006-May/413542.html). Thanks. Andrew -- https://mail.python.org/mailman/listinfo/python-list

Re: weird isinstance/issubclass behavior?

2012-11-29 Thread Terry Reedy
On 11/29/2012 9:59 AM, lars van gemerden wrote: Hi, I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. Basically if i import a class as: from mod1

Re: weird isinstance/issubclass behavior?

2012-11-29 Thread Ian Kelly
On Thu, Nov 29, 2012 at 9:07 AM, lars van gemerden wrote: > > PS: this is somewhat simpler than the actual case i've encountered, and > i haven't tested this exact case, but for now i hope this is enough to get > some of your insight. > > I know for sure that the imports both import the same file,

Re: weird isinstance/issubclass behavior?

2012-11-29 Thread lars van gemerden
On Thursday, November 29, 2012 3:59:37 PM UTC+1, lars van gemerden wrote: > Hi, > > > > I have encountered some strange behavior of isinstance(/issubclass): > depending on the import path used for classes i get different output, while > the classes i compare

Re: weird isinstance/issubclass behavior?

2012-11-29 Thread Chris Angelico
On Fri, Nov 30, 2012 at 1:59 AM, lars van gemerden wrote: > Basically if i import a class as: > > from mod1.mod2 import A > > or: > > from mod0.mod1.mod2 import A > > which both result in importing the same class, a call to isinstance(inst, A) > in anoth

weird isinstance/issubclass behavior?

2012-11-29 Thread lars van gemerden
Hi, I have encountered some strange behavior of isinstance(/issubclass): depending on the import path used for classes i get different output, while the classes i compare are in the same file. Basically if i import a class as: from mod1.mod2 import A or: from mod0.mod1.mod2 import

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 >

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

2012-11-08 Thread Peter Otten
file. In Python 2, I used "isinstance(p, file)" to > determine whether the parameter p is a file. In Python 3, the > returnvalue of open() is of type _io.TextIOWrapper, while the built-in > class file doesn't exist, so I can't use that code. > > Secondly, c

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 de

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

2012-11-08 Thread MRAB
hon 2, I used "isinstance(p, file)" to determine whether the parameter p is a file. In Python 3, the returnvalue of open() is of type _io.TextIOWrapper, while the built-in class file doesn't exist, so I can't use that code. Secondly, checking for the type is kind-of ugly, beca

isinstance(.., file) for Python 3

2012-11-08 Thread Ulrich Eckhardt
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 "isinstance(p,

Re: [Python-ideas] Allow isinstance second argument to be a set of types

2011-07-04 Thread Chris Rebert
On Mon, Jul 4, 2011 at 12:53 PM, MRAB wrote: > On 04/07/2011 20:41, Amaury Forgeot d'Arc wrote: >>> Le lundi 04 juillet 2011 à 10:52 -0700, Gregory P. Smith a écrit : note that a fast lookup implies exact type and not subclass making my point silly... at which point you're back to i

Re: [Python-ideas] Allow isinstance second argument to be a set of types

2011-07-04 Thread MRAB
On 04/07/2011 20:41, Amaury Forgeot d'Arc wrote: Hi, 2011/7/4 Antoine Pitrou: Le lundi 04 juillet 2011 à 10:52 -0700, Gregory P. Smith a écrit : note that a fast lookup implies exact type and not subclass making my point silly... at which point you're back to iterating so I suspect supporting

Re: seems like a bug in isinstance()

2011-05-07 Thread Gregory Ewing
dmitrey wrote: Yes, you are right. But I have to do it due to another issue I haven't solved yet: Python3 imports don't see files from same directory http://groups.google.com/group/comp.lang.python/browse_thread/thread/9470dbdacc138709# That's because the syntax for relative imports has ch

Re: seems like a bug in isinstance()

2011-05-07 Thread dmitrey
On May 7, 11:53 am, Gregory Ewing wrote: > Chris Rebert wrote: > > This is because you did `from Point import > > ...` in file2.py, whereas in file1.py you did `from > > openopt.kernel.Point import ...`. These 2 different ways of referring > > to the same module are sufficient to "trick"/"outsmart

Re: seems like a bug in isinstance()

2011-05-07 Thread Gregory Ewing
Chris Rebert wrote: This is because you did `from Point import ...` in file2.py, whereas in file1.py you did `from openopt.kernel.Point import ...`. These 2 different ways of referring to the same module are sufficient to "trick"/"outsmart" (C)Python and cause it to import the same module twice

Re: seems like a bug in isinstance()

2011-05-06 Thread Ian Kelly
On Fri, May 6, 2011 at 4:20 AM, dmitrey wrote: > Thanks Cris, however, I had understood reason of the bug and mere > informed Python developers of the bug to fix it. No you haven't. Few if any Python developers make a habit of reading this newsgroup. To actually report the issue so that it migh

Re: seems like a bug in isinstance()

2011-05-06 Thread dmitrey
ernel.Point import Point > > p = Point() > > > now let's pass p into a func from .../openopt/kernel/file2.py and > > check > > from Point import Point > > isinstance(p, Point) > > > So, it returns False! > > > p is , while Point is > openopt.kern

Re: seems like a bug in isinstance()

2011-05-06 Thread Chris Rebert
p into a func from .../openopt/kernel/file2.py and > check > from Point import Point > isinstance(p, Point) > > So, it returns False! > > p is , while Point is openopt.kernel.Point.Point at 0x2048e20> > > [Subject: seems like a bug in isinstance()] This is due to a pec

seems like a bug in isinstance()

2011-05-06 Thread dmitrey
hi all, suppose I've created a class Point in file .../openopt/kernel/Point.py Consider the code in file .../somewhere/file1.py from openopt.kernel.Point import Point p = Point() now let's pass p into a func from .../openopt/kernel/file2.py and check from Point import Point isinstanc

Re: exec, import and isinstance

2011-02-08 Thread Peter Otten
Michele Petrazzo wrote: > Hi all, > I'm playing with python internals and I'm discovering a strange behavior > of isinstance. Considering the following test case: > > test/bar.py > test/b.py > test/a/__init__.py > test/a/foo.py > > -- __init__.py -> e

exec, import and isinstance

2011-02-08 Thread Michele Petrazzo
Hi all, I'm playing with python internals and I'm discovering a strange behavior of isinstance. Considering the following test case: test/bar.py test/b.py test/a/__init__.py test/a/foo.py -- __init__.py -> empty --- foo.py: class foo: pass c = foo --- b.py def ret(): d = {}

Re: Unclear datetime.date type when using isinstance

2010-10-02 Thread Diez B. Roggisch
> >>>> import datetime > >>>> dt1 = datetime.datetime(2010, 10, 2) >>>> type(dt1) > >>>> isinstance(dt1, datetime.datetime) > True >>>> isinstance(dt1, datetime.date) > True > >>>> dt2 = datetime.date(2010, 10, 2) >

Re: Unclear datetime.date type when using isinstance

2010-10-02 Thread Chris Rebert
lead to this: > >>>> import datetime > >>>> dt1 = datetime.datetime(2010, 10, 2) >>>> type(dt1) > >>>> isinstance(dt1, datetime.datetime) > True >>>> isinstance(dt1, datetime.date) > True > >>>> dt2 = datetime.d

Unclear datetime.date type when using isinstance

2010-10-02 Thread Mailing List
tetime(2010, 10, 2) >>> type(dt1) >>> isinstance(dt1, datetime.datetime) True >>> isinstance(dt1, datetime.date) True >>> dt2 = datetime.date(2010, 10, 2) >>> type(dt2) >>> isinstance(dt2, datetime.datetime) False >>> isinstance(dt2, da

Re: Need instruction on how to use isinstance

2010-06-30 Thread alex23
Hans Mulder wrote: > There's also: hasattr(, '__call__').  It works in both 2.x and 3.x. Good work, Hans. I do find that to be a more pythonic approach, personally, being more concerned with an object's ability than its abstract type. -- http://mail.python.org/mailman/listinfo/python-list

Re: Need instruction on how to use isinstance

2010-06-30 Thread Hans Mulder
nt to do so via duck typing. Its rare. But it is there. That isinstance()/issubclass got a boost in power with the ABC's and registering, while at the same time the ability to introspect about the function-y callable-y ness of a function was removed? Makes no sense to me. But alas! There&

Re: Need instruction on how to use isinstance

2010-06-27 Thread Stephen Hansen
On 6/27/10 10:09 PM, alex23 wrote: Stephen Hansen wrote: What the hell? When did that show up? o.O (Did I not pay attention enough during the ABC conversations? It seemed so boring). The PEPs& post-release docs detailing Py3 changes were worth reading, it's noted in the sections on changes t

Re: Need instruction on how to use isinstance

2010-06-27 Thread Chris Rebert
ing, I >>> too have on occassion wanted to dispatch based on 'is callable? do x'. >>> Sometimes its not convenient to do so via duck typing. Its rare. But it >>> is there. That isinstance()/issubclass got a boost in power with the >>> ABC's and reg

Re: Need instruction on how to use isinstance

2010-06-27 Thread alex23
Stephen Hansen wrote: > What the hell? When did that show up? o.O (Did I not pay attention > enough during the ABC conversations? It seemed so boring). The PEPs & post-release docs detailing Py3 changes were worth reading, it's noted in the sections on changes to built-ins: http://www.python.org

Re: Need instruction on how to use isinstance

2010-06-27 Thread Stephen Hansen
On 6/27/10 9:47 PM, Chris Rebert wrote: There's always: isinstance(, collections.Callable) Why in Guido's name is that in the collections module of all places? What hath callability to do with container objects? What he said! Minus the blasphemy. It's Benevolent _Dictator

Re: Need instruction on how to use isinstance

2010-06-27 Thread Stephen Hansen
metimes its not convenient to do so via duck typing. Its rare. But it is there. That isinstance()/issubclass got a boost in power with the ABC's and registering, while at the same time the ability to introspect about the function-y callable-y ness of a function was removed? Makes no sense to me. But al

Re: Need instruction on how to use isinstance

2010-06-27 Thread Chris Rebert
ased on 'is callable? do x'. >> Sometimes its not convenient to do so via duck typing. Its rare. But it >> is there. That isinstance()/issubclass got a boost in power with the >> ABC's and registering, while at the same time the ability to introspect >> about t

Re: Need instruction on how to use isinstance

2010-06-27 Thread alex23
nt to do so via duck typing. Its rare. But it > is there. That isinstance()/issubclass got a boost in power with the > ABC's and registering, while at the same time the ability to introspect > about the function-y callable-y ness of a function was removed? Makes no > sense

Re: Need instruction on how to use isinstance

2010-06-27 Thread Stephen Hansen
On 6/27/10 8:04 PM, Steven W. Orr wrote: On 6/27/2010 10:25 PM, Stephen Hansen wrote: On 6/27/10 7:09 PM, Steven W. Orr wrote: So, my question is, what value can I use as the 2nd arg to isinstance to see if foo is a function? And while I'm on the subject, what types does isinstance not su

Re: Need instruction on how to use isinstance

2010-06-27 Thread Steven W. Orr
On 6/27/2010 10:25 PM, Stephen Hansen wrote: > On 6/27/10 7:09 PM, Steven W. Orr wrote: >> So, my question is, what value can I use as the 2nd arg to isinstance >> to see if >> foo is a function? And while I'm on the subject, what types does >> isinstance not >&

Re: Need instruction on how to use isinstance

2010-06-27 Thread MRAB
Steven W. Orr wrote: I need to test an argument for a few different types. I'm calling my function like this arf = MyFunc(list) What I want to do is to test for whether something is a string, tuple, list or function. It's that last one that's causing me a problem. if isins

Re: Need instruction on how to use isinstance

2010-06-27 Thread Stephen Hansen
On 6/27/10 7:09 PM, Steven W. Orr wrote: So, my question is, what value can I use as the 2nd arg to isinstance to see if foo is a function? And while I'm on the subject, what types does isinstance not support? Does it have to be a function? -- There's quite a few things which are

Need instruction on how to use isinstance

2010-06-27 Thread Steven W. Orr
I need to test an argument for a few different types. I'm calling my function like this arf = MyFunc(list) What I want to do is to test for whether something is a string, tuple, list or function. It's that last one that's causing me a problem. if isinstance(arg, (str, tup

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 >

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 >&

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 .

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

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-defin

"isinstance" question

2010-06-22 Thread John Nagle
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) >&g

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 wa

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

  1   2   3   >