What is considered an "advanced" topic in Python?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
73 messages Options
1234
Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Sturla Molden
Mike Driscoll <kyosohma at gmail.com> wrote:
> Hi,
>
> I've been asked on several occasions to write about intermediate or
> advanced topics in Python and I was wondering what the community
> considers to be "intermediate" or "advanced". I realize we're all growing
> in our abilities with the language, so this is going to be very
> subjective, but I am still curious what my fellow Python developers think about this topic.

Coroutines, metaclasses and decorators is probably high up on the obscurity
list. Writing C extension modules is high up there as well, not to mention
embedding Python and using subinterpreters.


Sturla



Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Jonathon Blake
In reply to this post by Mike Driscoll
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 29/05/2015 16:01, Mike Driscoll wrote:

>I was wondering what the community considers to be "intermediate" or "a
dvanced".

A python script that compiles python code.

jonathon
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBAgAGBQJVahA4AAoJEE1PKy9+kxplmwsP/R4wkjcgYZwG8wHj6Jp2Do4C
+y3raHDA7PNko9Y/HSsIr3aXyXm9Yy71px3HDYkOXXsD3aO+GXUS14RbQXynK7OX
95LD+S0U9AtJL5Jlywmvm/QJQw6UuGnPfQSs2Y1HiALjlvZYCDMvoycvT4GY4+oQ
6n4tq/3ExdP1inxcu0CwMm7ZL7CCvvy1S7JkVdr+r13BM4TlXCRH9BAvJNtLnbtA
SvP5iOYRkOTvxYOw8veiKXD1clXK2fy3x4SjBtWpK0XZKSu2akWwlD0qvbVkf9qz
/Cu9aP3BbV4VSzLFkOEKU6b4NoZK1Al3s3e4IVvAjPSWIXI5LsKwuVrqvVZ+06Uv
u0ZYyX/4fnpmKaJXvAMyRWULNA+pDaqvjBGgom3qtvT2i8nfYN1KBkMBMrG/ioXt
4BUkAMhOqRHvsPHT+GXrtVaj5wFj1Q9i174CMfINLelbmUlpNhOTLzobm2hnwD/c
hZAVsDUzAxAQjLYNDQV+AtE2Cw4D0YKUJfILE1T82CerSnibjy3Z60RZ703e5oRU
pa75Fm8HpjAmNS4tbvENSz0v0VH6howH1gQdqc2bVzKBGj9ArXiA8yByxRgZdVwI
vXJMzxzpMBL35ScYBhvVPnD0zroa8WBYgrhTWVJFFJpk+eTjUetalTrvkTFYWgQ+
QLsCw9+wUjX8kVJ8Yhx6
=wU1w
-----END PGP SIGNATURE-----


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Albert-jan Roskam
Metaclasses, abc, asyncio, ast, some of the dunder methods, eg __del__, weakref, perhaps gc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20150530/2199c36b/attachment.html>

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
In reply to this post by Jonathon Blake
On Sun, May 31, 2015 at 5:32 AM, jonathon <jonathon.blake at gmail.com> wrote:
> On 29/05/2015 16:01, Mike Driscoll wrote:
>
>>I was wondering what the community considers to be "intermediate" or "a
> dvanced".
>
> A python script that compiles python code.

What do you mean by "compiles"? Something as simple as an import
statement or exec call will compile arbitrary Python code, but if that
were what you meant, you'd have said "the exec() function". (The
implementation of namedtuple uses exec, and I think that would
definitely count as "advanced".) Or do you mean compiling to AST and
exploring the AST? That's a fairly reasonable intermediate topic,
though again, if that's what you meant, I'm sure you would have worded
it differently. The only other form of compilation I can think of is
going to an executable binary (the way a C compiler does), and that's
some serious work. I don't think PyPy is small enough to be called a
"script". :)

ChrisA


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

C.D. Reimer
In reply to this post by Mike Driscoll
On 5/29/2015 9:01 AM, Mike Driscoll wrote:
> I've been asked on several occasions to write about intermediate or advanced topics in Python and I was wondering what the community considers to be "intermediate" or "advanced".

I'm trying my hand at Cython (http://cython.org/). I just know enough of
the Python and C languages to understand the book, "Cython" by Kurt W.
Smith, but trying to understand this on a deeper level by re-reading
each chapter two or three times is enough to give me a headache.  Which
is fine since I have no use for a programming book that I can breeze
through. Then again, maybe this book wasn't the best book to learn
Cython from.

Chris R.


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Rustom Mody
In reply to this post by Mike Driscoll
On Friday, May 29, 2015 at 9:32:06 PM UTC+5:30, Mike Driscoll wrote:
> Hi,
>
> I've been asked on several occasions to write about intermediate or advanced topics in Python and I was wondering what the community considers to be "intermediate" or "advanced". I realize we're all growing in our abilities with the language, so this is going to be very subjective, but I am still curious what my fellow Python developers think about this topic.

[I will assume that this question makes sense in the context of introducing
someone (say beginner/learner/noob/etc) to the language]
The zen of python makes a subtle distinction between complex and complicated.
The answers seem to suggest that people are taking the complicated for the complex.

So at the risk of being simplistic (or glib) let me give a definition and a fact:
Def:
Complex is an 'act of God'
Complicated is a slip of man

Fact:
Programming languages (like programs) are made by humans... who err.

So while the err-ors of programmers are only too well-known and spelt 'bugs'
we dont spend enough time on language-bugs.
In a teaching context this only does injustice to the student because the
learner -- typically a kid -- suffering something or other and wondering
"Am I not fit for this?? Its so hard!" etc may only need a simple:
"Yeah the language designer goofed of on that one"

Trivial example:
C originally had only & | for both bitwise and logical operators.
Later the shortcircuit operators && || were added and kept adjacent in the
precedence table. MISTAKE.
The bitwise operators are conceptually arithmetic not logical, in that
x & MASK == MASK
can make sense only one way.
C makes sense of it the other way.

And so...
The most useful, honest line for a teacher to take is:
C has goofed on that one... So you need to parenthesize that
IOW the beginner is not wrong in writing
x & MASK == MASK
It is C that is wrong in choosing a meaningless parse of that

Now in all fairness python has less gotchas than C but it has its share.
So to my mind Steven's "iterators and generators" as intermediate represents
no so much an inherent complexity of the subject as an unfortunate complication
from sub-optimal design.

I would put generators as basic; iterators as intermediate.
The generator
def foo():
  yield 1
  yield 2
  yield 3

is 'just' the list [1,2,3] + laziness - memoization and should be closer
to lists in dificulty-level than to iterators.

The reason it seems to be at the level of iterators is related to language
definition, documentation, ontology eg the 'fact' that foo looks like a function
when conceptually/ontologically/implementationally it is not.

This is a language-goofup not student ignorance

One more minor nit with Steven's intermediate-list:
Command line parsing with optparse/argparse.
Well if you use an obese library you get what you ask.
Whats wrong with getopt?
Seems to me simple enough for a beginner to write simple scripts

As I said this is a little nit.
However the pedagogic principle is important.
argparse may be recommended whereas getopt may be deprecated [dunno if it is]
Nevertheless pedagogic expedience is not the same as real world best practice.

<Admission>
In a recent course I taught, I used a[0] and a[1:] to split arrays and write
recursive functions a la Haskell in Python.
Is it efficient? no
Is it idiomatic python? NO!
Is it good to do that? That depends on one's priority.
In mine, learning recursion is more important than learning idiomatic python
</Admission>


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
On Sun, May 31, 2015 at 1:30 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> <Admission>
> In a recent course I taught, I used a[0] and a[1:] to split arrays and write
> recursive functions a la Haskell in Python.
> Is it efficient? no
> Is it idiomatic python? NO!
> Is it good to do that? That depends on one's priority.
> In mine, learning recursion is more important than learning idiomatic python
> </Admission>

If recursion is more important than idiomatic Python, why are you
using a Python interpreter? Use Python to teach Python, and use
Haskell to teach Haskell!

ChrisA


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Rustom Mody
In reply to this post by Rustom Mody
On Sunday, May 31, 2015 at 9:55:45 AM UTC+5:30, Chris Angelico wrote:

> On Sun, May 31, 2015 at 1:30 PM, Rustom Mody wrote:
> > <Admission>
> > In a recent course I taught, I used a[0] and a[1:] to split arrays and write
> > recursive functions a la Haskell in Python.
> > Is it efficient? no
> > Is it idiomatic python? NO!
> > Is it good to do that? That depends on one's priority.
> > In mine, learning recursion is more important than learning idiomatic python
> > </Admission>
>
> If recursion is more important than idiomatic Python, why are you
> using a Python interpreter? Use Python to teach Python, and use
> Haskell to teach Haskell!

Heh! I would have expected less naivete from you Chris!
Firstly idiomatic haskell is as low priority (maybe more so) than idiomatic python

More to the point no language matches perfectly? everything that a learner needs to learn.
Can you write a kernel module in python? (Or Haskell?)
Can you see details of machine state and transitions in python?
Can you client-script a browser in python?
C is the best fit for the first
Assembly for the second
Javascript for the third.

And probably a dozen other languages for a dozen other key concepts.

Finally the idiomatic vs fundamentals is not a clear binary divide.
If I were interviewing someone claiming to be a C programmer who did not know
the difference between postfix and prefix ++, I'd be darned suspicious.
OTOH if someone who had written significant code in C simply refused to write
a '++' saying "Causes just too much trouble" most people would regard it as odd
but not unacceptable.

? or can. Thats G?del's second theorem for you


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
On Sun, May 31, 2015 at 2:46 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> More to the point no language matches perfectly? everything that a learner needs to learn.
> Can you write a kernel module in python? (Or Haskell?)
> Can you see details of machine state and transitions in python?
> Can you client-script a browser in python?
> C is the best fit for the first
> Assembly for the second
> Javascript for the third.

And if I want to build a web site, I won't write assembly code using
Python idioms. I will use Python. Why fight against a language?

ChrisA


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Rustom Mody
In reply to this post by Rustom Mody
On Sunday, May 31, 2015 at 10:28:39 AM UTC+5:30, Chris Angelico wrote:

> On Sun, May 31, 2015 at 2:46 PM, Rustom Mody wrote:
> > More to the point no language matches perfectly? everything that a learner needs to learn.
> > Can you write a kernel module in python? (Or Haskell?)
> > Can you see details of machine state and transitions in python?
> > Can you client-script a browser in python?
> > C is the best fit for the first
> > Assembly for the second
> > Javascript for the third.
>
> And if I want to build a web site, I won't write assembly code using
> Python idioms. I will use Python. Why fight against a language?

I thought I answered. I try again...
Because its the law.
Why does iron rust?
Why do cars/rockets not go faster than light?
Why do we age/shrivel/wither/die?
Because thats the law.
The law here is G?del's second theorem -- no single formalism can encompass all
formalisms.
Any real solution to a significant problem will use some (set of) languages
which will fit good here and not so good there.
Square pegs in round holes are not ideal
Yet every so often there's no choice but to use a wrench to hammer a screw
through a brick.

The C/assembly/javascript were extreme black-n-white examples.
Most real cases are more fuzzy.
Linus Torvalds thinks C++ (both the language and programmers) consititute
an asinine choice for writing kernel-code.
I am ready to bet that a democratic election on the subject will leave Torvalds
trailing.  ie the world's C++ programmers likely disagree with him.

Who is right?
Ive no idea. Do you?

The main point lost in this discussion is "What is the point?"
If you ask a beginner to write a function to give fibonacci numbers,
are fibonacci numbers the point?

I dont believe that python is 'the point' much more than fibonacci numbers.


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

random832@fastmail.us
In reply to this post by Chris Angelico
On Fri, May 29, 2015, at 13:08, Chris Angelico wrote:
> Also, I like talking about Fraction and
> Decimal for the simple reason that they're unobvious; you can poke
> around with Python and discover int and float, and if ever you need
> imaginary/complex numbers, you'll quickly come across complex, but you
> might use Python for years and not realize that decimal.Decimal even
> exists - nor when you'd want it.

Well, isn't that just a byproduct of what problem space you work in? If
someone _does_ know they need a rational or decimal type (e.g. someone
working with money who's clueful enough to know floats won't do),
they'll find these types relatively quickly from a google search -
there's no glut of third-party implementations, and neither is so
obscure nor easily written from scratch that people wouldn't search for
an existing implementation.

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
On Mon, Jun 1, 2015 at 1:18 PM,  <random832 at fastmail.us> wrote:

> On Fri, May 29, 2015, at 13:08, Chris Angelico wrote:
>> Also, I like talking about Fraction and
>> Decimal for the simple reason that they're unobvious; you can poke
>> around with Python and discover int and float, and if ever you need
>> imaginary/complex numbers, you'll quickly come across complex, but you
>> might use Python for years and not realize that decimal.Decimal even
>> exists - nor when you'd want it.
>
> Well, isn't that just a byproduct of what problem space you work in? If
> someone _does_ know they need a rational or decimal type (e.g. someone
> working with money who's clueful enough to know floats won't do),
> they'll find these types relatively quickly from a google search -
> there's no glut of third-party implementations, and neither is so
> obscure nor easily written from scratch that people wouldn't search for
> an existing implementation.

Yes, but how many people actually know they need a rational type? Just
now there's a thread on python-ideas that was based around the
expectation that a float could do that, which it can't; the OP just
naturally assumed that float was the data type he should be using.

ChrisA

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Laura Creighton-2
If you are giving a talk about Decimal -- and trying to stamp out the
inappropriate use of floats you have to first inform people that
what they learned as 'decimals' as children was not floating point,
despite the fact that we write them the same way.

If I ever get the time machine, I am going back in time and demand that
floating point numbers be expressed as 12345:678 instead of 12345.678
because it would save so much trouble.  Never has the adage 'It's not
what you don't know, that bites you.  It's what you know that ain't so.'
been more apt.

I have done much better in speaking about this topic to a bunch of
incredulous people if you next explain that scientists really don't
care about accuracy in their calculations.  (This will surprise them).
Most scentific calculations have some real world measurement in them,
and for most real world measurements, if you are getting even 5 digits
of precision, you are doing really, really, well.  This means that
scientists are going to be throwing away all the extra digits they get
out a a floating point representation, so they don't have to care how
accurate they are.  As long as their results are good in the first 5,
it won't matter.  (Depending on time constraints, a review of
significant figures -- what they are and what they mean is good here.)

It is really hard to get the concept of Decimal across to people who
already have that concept in their mind, but think it is called Float.
You have to first teach them that they don't know anything about Float
and get them to reboot their brains before you can install this new
knowledge.  Otherwise their brains will just overwrite your new knowledge
with 'ah, just use a Float' as soon as you stop speaking.  Same day,
even.

Laura


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Marko Rauhamaa
In reply to this post by Chris Angelico
Laura Creighton <lac at openend.se>:

> It is really hard to get the concept of Decimal across to people who
> already have that concept in their mind, but think it is called Float.

I can't remember running into this frustration.

However, I *constantly* run into engineers who don't understand what
zero means.


Marko

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
In reply to this post by Laura Creighton-2
On Mon, Jun 1, 2015 at 5:58 PM, Laura Creighton <lac at openend.se> wrote:

> If you are giving a talk about Decimal -- and trying to stamp out the
> inappropriate use of floats you have to first inform people that
> what they learned as 'decimals' as children was not floating point,
> despite the fact that we write them the same way.
>
> If I ever get the time machine, I am going back in time and demand that
> floating point numbers be expressed as 12345:678 instead of 12345.678
> because it would save so much trouble.  Never has the adage 'It's not
> what you don't know, that bites you.  It's what you know that ain't so.'
> been more apt.

While I agree that there are problems with conflating float with "real
number", I don't know that decimal.Decimal is actually going to solve
that either; and using a colon as the decimal separator won't solve
anything (the world already has two - "." in the US and "," in Europe
- and adding a third with the same semantics of separating the
greater-than-unit from the sub-unit digits won't instantly give new
meaning to the way numbers are handled), so I would be whacking you
over the head when you invent that time machine XKCD 716 style.

decimal.Decimal() has its own peculiarities, ways in which it isn't
the same as real numbers, so what's really needed is a talk about both
of them (and fractions.Fraction for completeness) and how they all
have their uses. People need to grok that what computers do with
numbers is never quite what they're used to from grade school
(although it can be close; bignum integer arithmetic is pretty
reliable).

ChrisA

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Laura Creighton-2
In a message of Mon, 01 Jun 2015 19:45:31 +1000, Chris Angelico writes:

>On Mon, Jun 1, 2015 at 5:58 PM, Laura Creighton <lac at openend.se> wrote:
>> If you are giving a talk about Decimal -- and trying to stamp out the
>> inappropriate use of floats you have to first inform people that
>> what they learned as 'decimals' as children was not floating point,
>> despite the fact that we write them the same way.
>>
>> If I ever get the time machine, I am going back in time and demand that
>> floating point numbers be expressed as 12345:678 instead of 12345.678
>> because it would save so much trouble.  Never has the adage 'It's not
>> what you don't know, that bites you.  It's what you know that ain't so.'
>> been more apt.
>
>While I agree that there are problems with conflating float with "real
>number", I don't know that decimal.Decimal is actually going to solve
>that either; and using a colon as the decimal separator won't solve
>anything (the world already has two - "." in the US and "," in Europe
>- and adding a third with the same semantics of separating the
>greater-than-unit from the sub-unit digits won't instantly give new
>meaning to the way numbers are handled), so I would be whacking you
>over the head when you invent that time machine XKCD 716 style.

You have missed my point.  What I want is for floats never to be
represented as '.' or ',' notation.  That way, when each naive
user writes his or her first program that deals with money, when
they look at their computer manual they will come to the section on
floating point numbers and they will all look like something they
have never seen before.  So they will read the section carefully
to see if this is what they want or need, and the section can nicely
say NEVER USE THIS FOR MONEY and they will know they are in the wrong
place.

The problem is that they take one look at a floating point number,
and think they know what it is from childhood, and use it.  They
don't know any better.  The notion that computer languages have
homophones, and just as the moles you have on your face and the
moles that burrow in your garden and the moles that you make out
of stone as a pier or a breakwater, numbers can work this way in
computers is deep, deep, deep in the 'this cannot be possible'
mindset of people who are learning to use computers.

I argued that when Decimal went into Python it should be called Money
(with an alias for Decimal for those who aren't using it for money) again
so that the people who don't know any better are more likely to end
up in the right place, but I lost that argument, too.

Laura


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Mark Lawrence
In reply to this post by Laura Creighton-2
On 01/06/2015 08:58, Laura Creighton wrote:

> If you are giving a talk about Decimal -- and trying to stamp out the
> inappropriate use of floats you have to first inform people that
> what they learned as 'decimals' as children was not floating point,
> despite the fact that we write them the same way.
>
> If I ever get the time machine, I am going back in time and demand that
> floating point numbers be expressed as 12345:678 instead of 12345.678
> because it would save so much trouble.  Never has the adage 'It's not
> what you don't know, that bites you.  It's what you know that ain't so.'
> been more apt.
>
> I have done much better in speaking about this topic to a bunch of
> incredulous people if you next explain that scientists really don't
> care about accuracy in their calculations.  (This will surprise them).
> Most scentific calculations have some real world measurement in them,
> and for most real world measurements, if you are getting even 5 digits
> of precision, you are doing really, really, well.  This means that
> scientists are going to be throwing away all the extra digits they get
> out a a floating point representation, so they don't have to care how
> accurate they are.  As long as their results are good in the first 5,
> it won't matter.  (Depending on time constraints, a review of
> significant figures -- what they are and what they mean is good here.)
>
> It is really hard to get the concept of Decimal across to people who
> already have that concept in their mind, but think it is called Float.
> You have to first teach them that they don't know anything about Float
> and get them to reboot their brains before you can install this new
> knowledge.  Otherwise their brains will just overwrite your new knowledge
> with 'ah, just use a Float' as soon as you stop speaking.  Same day,
> even.
>
> Laura
>

In the wonderful world of numbers I believe that things are looking up.
  I don't recall a new issue on the bug tracker for several months along
the lines of "Python can't do arithmetic properly".

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence


Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Chris Angelico
In reply to this post by Laura Creighton-2
On Mon, Jun 1, 2015 at 8:28 PM, Laura Creighton <lac at openend.se> wrote:
> You have missed my point.  What I want is for floats never to be
> represented as '.' or ',' notation.  That way, when each naive
> user writes his or her first program that deals with money, when
> they look at their computer manual they will come to the section on
> floating point numbers and they will all look like something they
> have never seen before.  So they will read the section carefully
> to see if this is what they want or need, and the section can nicely
> say NEVER USE THIS FOR MONEY and they will know they are in the wrong
> place.

The problem isn't the decimal separator, though, because floats can
have problems even without it (and can have no problems with a decimal
separator). If you want to distinguish "computer numbers" from "real
numbers", you'd do better to pick a different set of symbols for them
- or at least a different numerical base. If all literals were written
in octal, people would understand that there's something special going
on here. But would that really help?

ChrisA

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Dave Farrance
In reply to this post by Chris Angelico
Laura Creighton <lac at openend.se> wrote:

>If you are giving a talk about Decimal -- and trying to stamp out the
>inappropriate use of floats you have to first inform people that
>what they learned as 'decimals' as children was not floating point,
>despite the fact that we write them the same way. ...

To be fair, prior to electronic computers, they were essentially
synonymous.  It's a bit like finance experts chiding people for mixing
"cost" with "price".

>>> Decimal('0.3')
Decimal('0.3')
>>> Decimal(0.3)
Decimal('0.299999999999999988897769753748434595763683319091796875')

Reply | Threaded
Open this post in threaded view
|

What is considered an "advanced" topic in Python?

Marko Rauhamaa
Dave Farrance <DaveFarrance at OMiTTHiSyahooANDTHiS.co.uk>:

>>>> Decimal('0.3')
> Decimal('0.3')
>>>> Decimal(0.3)
> Decimal('0.299999999999999988897769753748434595763683319091796875')

At first I thought I had never dealt with money in Python nor made use
of Decimal. Then I remembered I *did* deal with money in one program and
wondered how I handled it. Sure enough:

        newdistr[clubacct] = decimal.Decimal("%.2f" % amount)


Marko

1234