Do any of you recommend Python as a first programming language?

S

Steven D'Aprano

The fact that .func_name (which is writeable) is not used at first
surprised me until I remembered that code objects can potentially be
used by multiple function objects and hence are not connected to any one
in particular.

How does that happen?

And if it is the case, what's the justification for giving them a co_name
attribute? Surely the name of the function should be that of the function
object, not of one of the shared parts?
 
A

Arnaud Delobelle

How does that happen?

Like this:
... def foo(y): return x+y
... return foo
...<code object foo at 0x73530, file "<stdin>", line 2>

Of course foo1 and foo2 are not the same thing:
10

And if it is the case, what's the justification for giving them a co_name
attribute? Surely the name of the function should be that of the function
object, not of one of the shared parts?
'foo'

As seen above, func.__name__ and func.func_code.co_name are the same
thing (until tampered with).
 
S

Steven D'Aprano

Like this:

... def foo(y): return x+y
... return foo
...
<code object foo at 0x73530, file "<stdin>", line 2>


Ah, that makes sense. And obvious in hindsight.

Of course foo1 and foo2 are not the same thing:

Naturally not. They are different functions. The fact that they share a
small part (func_code) is irrelevant. There's more to a function than the
code object.


'foo'

As seen above, func.__name__ and func.func_code.co_name are the same
thing (until tampered with).

Yes, but what I'm asking is why the code objects have a co_name
attribute. And even if there's a good reason for code objects to have a
name, why do tracebacks use func.func_code.co_name instead of
func.__name__?
 
A

Arnaud Delobelle

On Mar 24, 2:01 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
[...]
Yes, but what I'm asking is why the code objects have a co_name
attribute. And even if there's a good reason for code objects to have a
name, why do tracebacks use func.func_code.co_name instead of
func.__name__?

From what I remember when I looked at the source: stack frames execute
code objects, not functions. They don't know what function has
spawned them, only what code object they are executing. In fact when
one thinks of it, it makes more sense for code objects to have a name
(taken from the def statement) than for function objects, as there is
exactly one code object for every def statement.
 
B

bearophileHUGS

Paul Rubin>It's at least pretty good. It's not ideal, but nothing
ever is.<

I agree. At the moment Python is among the best languages for that,
but it's not perfect for that purpose. I think newbies may need a bit
more structured language, where you have to put things in a certain
order to have a working program. I think this may teach them some good
programming habits. Pascal is a bit too much old today, but it helps
learn how to program in a tidy way. I am not sure about this, I'd like
to have more data to be able to sort out this freedom/structure
alternative regarding teaching languages.
Another possible downside of Python is that it doesn't allow you to
know very well what's under the language, so you can't use pointers
and memory very well, so you can't learn those things very well. You
need a different language for that, like C (or Pascal) or assembly,
but such languages are useful later, and less as very first languages,
especially for very young people.


Paul Rubin>Chris Okasaki (of functional data structures fame) has an
interesting blog post about why indentation-based structuring is a big
help for teaching:<

I have quite appreciated that article, and other people are doing
similar things for C and C++ (I'd like to do something similar for D):
http://blog.micropledge.com/2007/09/nobraces/
And OCaml:
http://people.csail.mit.edu/mikelin/ocaml+twt/


Arnaud Delobelle>My other 'coming of age' was when I took a lambda-
calculus course at university. I felt like a man who's had a black
and white TV set all his life and watches colour TV for the first
time.<

Scheme-like languages are surely interesting, and eventually a
programmer can learn one of them, but I think as a very first language
Python is fitter, because its syntax is more compatible with a normal
little trained human mind.



7stud>Beginning programmers in grades 9-12 are not going to understand
issues like that, and it would be a mistake to try and introduce
them. Beginning programmers should be concentrating their efforts on
learning the syntax of a language and basic constructs like for-loops
and if statements.<

This is wrong. Some languages like Scheme are quite fit for young
people, and they don't even need a for loop. A 7-9 years old person is
able to understand quite a lot, and computer science and programming
aren't based on syntax. They are more based on the fun of problem
solving, and similar things. Syntax is important because it's the tool
that allows you to tell the computer how to compute the results of the
problems you have (usually) already solved, but it can't be the
purpose, even in a first course of programming. When you want to teach
painting to a 6 year old child your purpose isn't teaching her/him all
about the structure of the canvas and brushes and the chemistry of the
pigments, but how to have fun putting all the colors on the canvas/
paper, trying to create some image, for fun. (On the other hand you
can teach to love chemistry to a 6-8 year old person, showing how much
it can be fun, if you start talking about the chemistry behind the
colors of fireworks, but that's not panting anymore).


Ben C>Why does Python not have a switch or until statement?<

Maybe because it thinks syntax minimalism is a virtue, or maybe
because they aren't easy to add (try to design a do-while using Python-
like syntax).


Ben C>Why are very common objects (stack, queue, linked list) not
builtin? etc.<

Because it was a small language, used for scripting purposes, I think.
You need those data structures if you want higher performance (in
speed and/or memory used) but performance was not one of the main
purposes of Python.
Another purpose for those data structures is to teach them, but a good
teaching language is probably one that allows you to write your own
versions of them.
Today the collections module and other built-in modules gives you some
of those data structures, you just need to import them, so using them
isn't much a hassle, even if they aren't built in.


Aahz>The problem with lambda is that too often it results in clutter<

Probably I am missing your point, but I'd like to remind people the
syntax for lambdas in the last C# versions:
x, y => x * 2 + y
Instead of the Python syntax:
lambda x, y: x * 2 + y
I think that C# syntax is nice and readable enough.


Terry Reedy>[lot of things] Yes, fundamentally different categories of
types are (sensibly) treated differently with repect to definition and
namespace names, but calling that 'disharmony' depends on the
listener.<

Thank you for your interesting explanation.

Bye,
bearophile
 
T

Terry Reedy

Arnaud Delobelle said:
From what I remember when I looked at the source: stack frames execute
code objects, not functions. They don't know what function has
spawned them, only what code object they are executing. In fact when
one thinks of it, it makes more sense for code objects to have a name
(taken from the def statement) than for function objects, as there is
exactly one code object for every def statement.

This is what I tried to say before. Function objects have the attributes
needed to call the code. Code objects have attributes needed to execute
the code. Both have a name for str() and repr().

tjr
 
S

Simon Brunning

Python is a programming language. It can be used for scripting, but
that's not all it can do. Describing it as a "scripting language" is like
describing a fully-equipped professional kitchen as "a left-over warming
room".

+1 QOTW.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top