Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52313 > unrolled thread

Re: Python Basic Doubt

Started byXavi <jarabal@gmail.com>
First post2013-08-10 20:00 +0200
Last post2013-08-12 01:08 +0000
Articles 4 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Python Basic Doubt Xavi <jarabal@gmail.com> - 2013-08-10 20:00 +0200
    Re: Python Basic Doubt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-10 19:32 +0000
      Re: Python Basic Doubt Xavi <jarabal@gmail.com> - 2013-08-11 18:58 +0200
        Re: Python Basic Doubt Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-12 01:08 +0000

#52313 — Re: Python Basic Doubt

FromXavi <jarabal@gmail.com>
Date2013-08-10 20:00 +0200
SubjectRe: Python Basic Doubt
Message-ID<mailman.432.1376157806.1251.python-list@python.org>
Hello,

El 10/08/2013 18:40, Tim Chase escribió:
> Generally, if you are using the "is" operator to compare against
> anything other than None, you're doing it wrong. There are exceptions
> to this, but it takes knowing the particulars.

Now I have one doubt, I use 'is' to compare basic types in python 3, for example .-

v = []
if type(v) is list:
     print('Is list...')

Because I think it is more clear and faster than .-
type(v) == [].__class__  ... or ... isinstance(v, list)

Is this correct?
Thanks.
-- 
Xavi

[toc] | [next] | [standalone]


#52317

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-10 19:32 +0000
Message-ID<52069539$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#52313
On Sat, 10 Aug 2013 20:00:58 +0200, Xavi wrote:

> Now I have one doubt, I use 'is' to compare basic types in python 3, for
> example .-
> 
> v = []
> if type(v) is list:
>      print('Is list...')

No, do not do this. This is unnecessarily restrictive.

> Because I think it is more clear and faster than .- 

Clear? Maybe. Clear, but does the wrong thing. Using type rejects 
subclasses, which is normally a bad idea. Using isinstance accepts 
subclasses, which is what we nearly always should do.

As for being faster -- who cares? The difference between calling type and 
calling isinstance is about 0.02 microseconds on my slow computer. You 
should not try to optimize things which are so unimportant.

The first rule of optimization: Don't do it.
For experts only: Don't do it yet.

Until you have profiled your application, and discovered calling 
isinstance is the bottleneck making your application too slow, you are 
wasting your time trying to guess what will make it go faster.



> type(v) == [].__class__

You should not do that either. Names starting and ending with double-
underscore are reserved for Python. They are not quite private 
implementation details, but you almost never need to use them directly.

Why keep a dog and then bark yourself? Python will check __class__ for 
you, when and if needed. That is not your job. It is very rare to need to 
use __dunder__ attributes by hand.

> ... or ... isinstance(v, list)

That's the right solution, 99.9% of the time.

Actually, 99% of the time you should not call isinstance at all, but just 
catch any errors that occur; or better still, only catch them if you can 
do something about it. Otherwise, just allow the exception to propagate 
to the caller, who may catch it. Calling isinstance should be rare; 
calling type to check for an exact class even rarer.


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#52394

FromXavi <jarabal@gmail.com>
Date2013-08-11 18:58 +0200
Message-ID<mailman.487.1376248139.1251.python-list@python.org>
In reply to#52317
Thanks to all for your answers,

I guess it is more flexible with isinstance (the duck test :)
I'm going to change the type checks.

Respect to the "Names starting and ending with double-underscore".
I don't know how to get the name of a classe without them.
obj.__class__.__name__

Thanks.
-- 
Xavi

[toc] | [prev] | [next] | [standalone]


#52395

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-08-12 01:08 +0000
Message-ID<52083587$0$30000$c3e8da3$5496439d@news.astraweb.com>
In reply to#52394
On Sun, 11 Aug 2013 18:58:25 +0200, Xavi wrote:

> Respect to the "Names starting and ending with double-underscore". I
> don't know how to get the name of a classe without them.
> obj.__class__.__name__

I didn't say you should *never* use them, but most of the time, you don't.

However type(obj).__name__ should be better.


-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web