Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
Is there any consensus?
I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
On 3/03/23 9:54 am, Ian Pilcher wrote:
I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
In such cases I'd probably go for type(x), because it looks less
ugly.
x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.
On 3/03/23 9:54 am, Ian Pilcher wrote:
I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
In such cases I'd probably go for type(x), because it looks less
ugly.
x.__class__ *might* be slightly more efficient, as it avoids a
global lookup and a function call. But as always, measurement
would be required to be sure.
Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
On 02/03/2023 20:54, Ian Pilcher wrote:
Seems like an FAQ, and I've found a few things on StackOverflow that discuss the technical differences in edge cases, but I haven't found anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
I think avoiding dunder methods is generally considered more Pythonic.
But in this specific case using isinstance() is almost always
the better option. Testing for a specific class is likely to break
down in the face of subclasses. And in Python testing for static types
should rarely be necessary since Python uses duck typing
and limiting things to a hard type seriously restricts your code.
Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
On Fri, 3 Mar 2023 at 20:44, Alan Gauld wrote:
On 02/03/2023 20:54, Ian Pilcher wrote:
Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
I think avoiding dunder methods is generally considered more Pythonic.
But in this specific case using isinstance() is almost always
the better option.
Using isinstance is very different from querying the type of an object though. They're used for different purposes. And obj.__class__ and
type(obj) are different too, which is why the OP specifically narrowed
this down to the situations where you know they're the same.
Seems like an FAQ, and I've found a few things on StackOverflow that
discuss the technical differences in edge cases, but I haven't found
anything that talks about which form is considered to be more Pythonic
in those situations where there's no functional difference.
I leave you with the question of the day. Was Voldemort pythonic?
I do not buy into any concept about something being pythonic or not.
Python has grown too vast and innovated quite a bit, but also borrowed from others and vice versa.
There generally is no universally pythonic way nor should there be. Is there a C way
and then a C++ way and an R way or JavaScript
or does only python a language with a philosophy of what is the
pythonic way?
Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python").
I do not buy into any concept about something being pythonic or not.from
Python has grown too vast and innovated quite a bit, but also borrowed
others and vice versa.there
There generally is no universally pythonic way nor should there be. Is
a C way
and then a C++ way and an R way or JavaScript
or does only python a language with a philosophy of what is the
pythonic way?
On 2023-03-03 13:51:11 -0500, avi.e.gross@gmail.com wrote:
No. Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python"). Also quite similar to natural languages where you can guess the native language of an L2
speaker by their accent and phrasing.
Of course each language has commonly used idioms
Of course each language has commonly used idioms as C with pointer
arithmetic and code like *p++=*q++ but my point is that although I live near a seaway and from where C originated, I am not aware of words like "c-way" or "scenic" as compared to the way people keep saying "pythonic".
On 2023-03-03 13:51:11 -0500, avi.e.gross@gmail.com wrote:
No. Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python"). Also quite similar to natural languages where you can guess the native language of an L2
speaker by their accent and phrasing.
Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python").
Of course each language has commonly used idioms
I don't know, Thomas. For some simple programs, there is some evolutionary benefit by starting with what you know and gradually growing from there. He first time you need to do something that seems to need a loop in python, there are loops to choose from.
But as noted in a recent discussion, things are NOT NECESSARILY the same
even with something that simple. Did your previous languages retain
something like the loop variable outside the loop? What are your new scoping rules? Do you really want to keep using global variables, and so on.
And, another biggie is people who just don't seem aware of what comes easily in the new language. I have seen people from primitive environments set up programs with multiple arrays they process the hard way instead of using
some forms of structure like a named tuple or class arranged in lists or use a multidimensional numpy/pandas kind of data structure.
So ignoring the word pythonic as too specific, is there a way to say that something is the way your current language supports more naturally?
Yes, there are sort of fingerprints in how people write. Take the python concept of truthy and how some people will still typically add a test for equality with True. That may not be pythonic to some but is there much harm in being explicit so anyone reading the code better understands what it doe?
I have to wonder what others make of my code as my style is likely to be considered closer to "eclectic" as I came to python late and found an expanding language with way too many ways to do anything and can choose. But I claim that too is pythonic!
-----Original Message-----
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org> On Behalf Of Thomas Passin
Sent: Saturday, March 4, 2023 1:09 PM
To: python-list@python.org
Subject: Re: Which more Pythonic - self.__class__ or type(self)?
On 3/4/2023 2:47 AM, Peter J. Holzer wrote:
Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a
language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python").
What Peter didn't say is that this statement is usually used in a
disparaging sense. It tends to imply that a person can write (or is
writing) awkward or inappropriate code anywhere.
I think you are over-thinking this, Avi :)
I don't know, Thomas. For some simple programs, there is some evolutionary benefit by starting with what you know and gradually growing from there.He
first time you need to do something that seems to need a loop in python, there are loops to choose from.scoping
But as noted in a recent discussion, things are NOT NECESSARILY the same
even with something that simple. Did your previous languages retain
something like the loop variable outside the loop? What are your new
rules? Do you really want to keep using global variables, and so on.easily
And, another biggie is people who just don't seem aware of what comes
in the new language. I have seen people from primitive environments set up programs with multiple arrays they process the hard way instead of usinguse
some forms of structure like a named tuple or class arranged in lists or
a multidimensional numpy/pandas kind of data structure.harm
So ignoring the word pythonic as too specific, is there a way to say that something is the way your current language supports more naturally?
Yes, there are sort of fingerprints in how people write. Take the python concept of truthy and how some people will still typically add a test for equality with True. That may not be pythonic to some but is there much
in being explicit so anyone reading the code better understands what itdoe?
I have to wonder what others make of my code as my style is likely to be considered closer to "eclectic" as I came to python late and found an expanding language with way too many ways to do anything and can choose.But
I claim that too is pythonic!
-----Original Message-----On
From: Python-list <python-list-bounces+avi.e.gross=gmail.com@python.org>
Behalf Of Thomas Passin
Sent: Saturday, March 4, 2023 1:09 PM
To: python-list@python.org
Subject: Re: Which more Pythonic - self.__class__ or type(self)?
On 3/4/2023 2:47 AM, Peter J. Holzer wrote:
Even before Python existed there was the adage "a real programmer
can write FORTRAN in any language", indicating that idiomatic usage of a
language is not governed by syntax and library alone, but there is a
cultural element: People writing code in a specific language also read
code by other people in that language, so they start imitating each
other, just like speakers of natural languages imitate each other.
Someone coming from another language will often write code which is
correct but un-idiomatic, and you can often guess which language they
come from (they are "writing FORTRAN in Python").
What Peter didn't say is that this statement is usually used in a
disparaging sense. It tends to imply that a person can write (or is
writing) awkward or inappropriate code anywhere.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 546 |
Nodes: | 16 (2 / 14) |
Uptime: | 30:47:09 |
Calls: | 10,391 |
Calls today: | 2 |
Files: | 14,064 |
Messages: | 6,417,098 |