syntax for code blocks
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Tue May 1 20:09:07 EDT 2012
On Tue, 01 May 2012 19:07:58 +0200, Kiuhnm wrote:
> On 5/1/2012 17:11, Steven D'Aprano wrote:
>>> My way
>>> ------
>>>
>>> with func(some_args)<< ':dict':
>>> with when_odd as 'n':
>>> pass
>>> with when_prime as 'n':
>>> pass
>>
>>
>> If you actually try that, you will see that it cannot work. You get:
>>
>> SyntaxError: can't assign to literal
>
> If you had read the module's docstring
What module?
> you would know that the public version uses
> with when_odd << 'n':
> pass
Then why didn't you write that instead of something that gives
SyntaxError? Not once, but EIGHT times.
>> Have you actually tried to use these code blocks of yours? I asked you
>> for a *working* example earlier, and you replied with examples that
>> failed with multiple NameErrors and no hint as to how to fix them. And
>> now you give an example that fails with SyntaxError.
>
> The examples I gave you work perfectly.
Except they don't.
Look, this isn't difficult. I asked for *working* examples, you gave
examples that give NameError. Some of those errors are easy to fix, e.g.
by importing the random and re modules. Some of them aren't. What are
"ris", "repl", "_return", "sorted1", "key"? Where do they come from? What
value should I give them to make your code work?
Code doesn't magically start to work because you declare that it does.
Anyone who takes the time to copy and paste your examples into a fresh
Python interactive session will see that they don't.
Here's one of your earlier examples. I've removed the unnecessary
indentation and made the obvious import, so all it takes to run it is to
copy it and paste into the Python prompt:
import random
numbers = [random.randint(1, 100) for i in range(30)]
with sorted1 << sorted(numbers) << key << 'x':
if x <= 50:
_return(-x)
else:
_return(x)
print(sorted1)
If you try it, you get
NameError: name 'sorted1' is not defined
Fix that (how?) and you'll get another NameError, for 'key', and then a
third, for '_return'.
The syntax isn't very clear. If I had to guess what it does, I'd predict
that maybe it sorts the random list and negates everything <= 50, e.g.
given input [5, 99, 34, 88, 70, 2] it returns [-2, -5, -34, 70, 88, 99].
Obviously that's wrong. You say it should return [34, 5, 2, 70, 88, 99].
The Pythonic way to get that result is:
import random
numbers = [random.randint(1, 100) for i in range(30)]
sorted(numbers, key=lambda x: -x if x <= 50 else x)
which is much more straightforward and obvious than yours, and presumably
much faster. So even if I could get your example working, it would not be
very persuasive.
> It's clear that you don't even
> have the vaguest idea of how my module works or, otherwise, you'd know
> what you're doing wrong.
Well duh. What module? Where do I find it? How am I supposed to know what
this module is when you haven't mentioned it?
Believe it or not, the world does not revolve around you. We cannot see
what is in your head. If we ask for a WORKING EXAMPLE, you need to give
an example that includes EVERYTHING necessary to make it work.
[...]
> Talking with you is a real pain. You're always partial in your opinions
> and this urge of yours to criticize other's work makes you look dumb or
> hopefully just lazy.
> I can't stand people like you who don't even have the decency of taking
> the time to read the documentation of a project and just run their mouth
> without any concern for facts.
What project? You're the one who doesn't tell us what project we're
supposed to use, and yet *I'm* the dumb one.
This is comedy gold.
> What I can't stand is that if I won't reply to your posts other lazy
> people will believe the nonsense you say, but I'll have to live with
> that because I've wasted enough time with you.
Whatever man. It's no skin off my nose. I've tried really hard to give
your proposal a fair go, but my care factor is rapidly running out if you
can't even be bothered to ensure your examples use legal Python syntax.
--
Steven
More information about the Python-list
mailing list