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


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

Show off your Python chops and compete with others

Started byNathaniel Sokoll-Ward <nathanielsokollward@gmail.com>
First post2013-11-06 16:00 -0800
Last post2013-11-12 09:20 -0800
Articles 20 on this page of 32 — 16 participants

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


Contents

  Show off your Python chops and compete with others Nathaniel Sokoll-Ward <nathanielsokollward@gmail.com> - 2013-11-06 16:00 -0800
    Re: Show off your Python chops and compete with others Andrew Cooper <root@127.0.0.1> - 2013-11-07 00:13 +0000
      Re: Show off your Python chops and compete with others Roy Smith <roy@panix.com> - 2013-11-06 19:24 -0500
        Re: Show off your Python chops and compete with others Andrew Cooper <root@127.0.0.1> - 2013-11-07 00:28 +0000
          Re: Show off your Python chops and compete with others Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-07 00:38 +0000
    Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-07 11:59 +1100
    Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-07 12:04 +1100
      Re: Show off your Python chops and compete with others John Nagle <nagle@animats.com> - 2013-11-06 17:31 -0800
        Re: Show off your Python chops and compete with others Tim Chase <python.list@tim.thechases.com> - 2013-11-06 21:19 -0600
          Re: Show off your Python chops and compete with others Nathaniel Sokoll-Ward <nathanielsokollward@gmail.com> - 2013-11-07 10:38 -0800
            Re: Show off your Python chops and compete with others Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-07 18:48 +0000
            Re: Show off your Python chops and compete with others Alister <alister.ware@ntlworld.com> - 2013-11-07 19:08 +0000
              Re: Show off your Python chops and compete with others Roy Smith <roy@panix.com> - 2013-11-07 20:02 -0500
                Re: Show off your Python chops and compete with others alex23 <wuwei23@gmail.com> - 2013-11-08 11:49 +1000
                  Re: Show off your Python chops and compete with others Roy Smith <roy@panix.com> - 2013-11-07 20:54 -0500
                    Re: Show off your Python chops and compete with others alex23 <wuwei23@gmail.com> - 2013-11-08 12:08 +1000
                      Re: Show off your Python chops and compete with others Roy Smith <roy@panix.com> - 2013-11-07 21:18 -0500
                        Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-08 13:26 +1100
                        Re: Show off your Python chops and compete with others Tim Chase <python.list@tim.thechases.com> - 2013-11-07 22:05 -0600
                          Re: Show off your Python chops and compete with others Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-11-08 05:26 +0000
                        Re: Show off your Python chops and compete with others Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-08 09:23 +0000
                  Re: Show off your Python chops and compete with others Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2013-11-08 19:47 +1300
            Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-08 09:55 +1100
          Re: Show off your Python chops and compete with others jskirst@gmail.com - 2013-11-07 11:02 -0800
            Re: Show off your Python chops and compete with others Tim Chase <python.list@tim.thechases.com> - 2013-11-07 13:30 -0600
            Re: Show off your Python chops and compete with others 88888 Dihedral <dihedral88888@gmail.com> - 2013-11-07 16:19 -0800
    Re: Show off your Python chops and compete with others MRAB <python@mrabarnett.plus.com> - 2013-11-07 01:12 +0000
    Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-07 12:17 +1100
    Re: Show off your Python chops and compete with others John Ladasky <john_ladasky@sbcglobal.net> - 2013-11-06 17:55 -0800
      Re: Show off your Python chops and compete with others Chris Angelico <rosuav@gmail.com> - 2013-11-07 13:07 +1100
    Re: Show off your Python chops and compete with others Omar Abou Mrad <omar.aboumrad@gmail.com> - 2013-11-08 09:09 +0200
    Re: Show off your Python chops and compete with others Nathaniel Sokoll-Ward <nathanielsokollward@gmail.com> - 2013-11-12 09:20 -0800

Page 1 of 2  [1] 2  Next page →


#58601 — Show off your Python chops and compete with others

FromNathaniel Sokoll-Ward <nathanielsokollward@gmail.com>
Date2013-11-06 16:00 -0800
SubjectShow off your Python chops and compete with others
Message-ID<82328ec8-7178-4a17-abfe-13d444fab9cf@googlegroups.com>
Thought this group would appreciate this: www.metabright.com/challenges/python

MetaBright makes skill assessments to measure how talented people are at different skills. And recruiters use MetaBright to find outrageously skilled job candidates.

Python is a new area of expertise for us. We make "Challenges" for a bunch of languages and we're excited to finally have Python released. Give it a shot -- I'd love to hear what you think.

[toc] | [next] | [standalone]


#58604

FromAndrew Cooper <root@127.0.0.1>
Date2013-11-07 00:13 +0000
Message-ID<jWAeu.102858$rN3.45213@fx21.am4>
In reply to#58601
On 07/11/2013 00:00, Nathaniel Sokoll-Ward wrote:
> Thought this group would appreciate this: www.metabright.com/challenges/python
> 
> MetaBright makes skill assessments to measure how talented people are at different skills. And recruiters use MetaBright to find outrageously skilled job candidates.
> 
> Python is a new area of expertise for us. We make "Challenges" for a bunch of languages and we're excited to finally have Python released. Give it a shot -- I'd love to hear what you think.
> 

"What is the correct number of spaces for indentation in Python?"

I presume the question should be more along the lines of "What does PEP8
say?", because all answers are correct.

"String literals are written with what?"

The answer is not "ALl of these answers are correct"


So two of of 7 questions with wrong answers so far...

~Andrew

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


#58605

FromRoy Smith <roy@panix.com>
Date2013-11-06 19:24 -0500
Message-ID<roy-4A42DF.19240806112013@news.panix.com>
In reply to#58604
In article <jWAeu.102858$rN3.45213@fx21.am4>,
 Andrew Cooper <root@127.0.0.1> wrote:

> On 07/11/2013 00:00, Nathaniel Sokoll-Ward wrote:
> > Thought this group would appreciate this: 
> > www.metabright.com/challenges/python
> > 
> > MetaBright makes skill assessments to measure how talented people are at 
> > different skills. And recruiters use MetaBright to find outrageously 
> > skilled job candidates.
> > 
> > Python is a new area of expertise for us. We make "Challenges" for a bunch 
> > of languages and we're excited to finally have Python released. Give it a 
> > shot -- I'd love to hear what you think.
> > 
> 
> "What is the correct number of spaces for indentation in Python?"




What does the following code do?
def a(b, c, d): pass

My answer: "Defines a function which returns None", but that isn't one 
of the choices.

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


#58606

FromAndrew Cooper <root@127.0.0.1>
Date2013-11-07 00:28 +0000
Message-ID<r8Beu.11907$ZJ5.3767@fx09.am4>
In reply to#58605
On 07/11/2013 00:24, Roy Smith wrote:
> In article <jWAeu.102858$rN3.45213@fx21.am4>,
>  Andrew Cooper <root@127.0.0.1> wrote:
> 
>> On 07/11/2013 00:00, Nathaniel Sokoll-Ward wrote:
>>> Thought this group would appreciate this: 
>>> www.metabright.com/challenges/python
>>>
>>> MetaBright makes skill assessments to measure how talented people are at 
>>> different skills. And recruiters use MetaBright to find outrageously 
>>> skilled job candidates.
>>>
>>> Python is a new area of expertise for us. We make "Challenges" for a bunch 
>>> of languages and we're excited to finally have Python released. Give it a 
>>> shot -- I'd love to hear what you think.
>>>
>>
>> "What is the correct number of spaces for indentation in Python?"
> 
> 
> 
> 
> What does the following code do?
> def a(b, c, d): pass
> 
> My answer: "Defines a function which returns None", but that isn't one 
> of the choices.
> 

"Which is a correct way to perform exponentiation in Python?"

1) math.pow(a,b)
2) a^b
3) a*2b
4) None of the other responses are correct

Apparently I was wrong by answering 4), and 1) is the expected answer.
Clearly the author doesn't know about the ** operator in python.


It appears that no serious python coders were consulted when writing
these questions.

~Andrew

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


#58608

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-07 00:38 +0000
Message-ID<mailman.2104.1383784731.18130.python-list@python.org>
In reply to#58606
On 07/11/2013 00:28, Andrew Cooper wrote:
> On 07/11/2013 00:24, Roy Smith wrote:
>> In article <jWAeu.102858$rN3.45213@fx21.am4>,
>>   Andrew Cooper <root@127.0.0.1> wrote:
>>
>>> On 07/11/2013 00:00, Nathaniel Sokoll-Ward wrote:
>>>> Thought this group would appreciate this:
>>>> www.metabright.com/challenges/python
>>>>
>>>> MetaBright makes skill assessments to measure how talented people are at
>>>> different skills. And recruiters use MetaBright to find outrageously
>>>> skilled job candidates.
>>>>
>>>> Python is a new area of expertise for us. We make "Challenges" for a bunch
>>>> of languages and we're excited to finally have Python released. Give it a
>>>> shot -- I'd love to hear what you think.
>>>>
>>>
>>> "What is the correct number of spaces for indentation in Python?"
>>
>>
>>
>>
>> What does the following code do?
>> def a(b, c, d): pass
>>
>> My answer: "Defines a function which returns None", but that isn't one
>> of the choices.
>>
>
> "Which is a correct way to perform exponentiation in Python?"
>
> 1) math.pow(a,b)
> 2) a^b
> 3) a*2b
> 4) None of the other responses are correct
>
> Apparently I was wrong by answering 4), and 1) is the expected answer.
> Clearly the author doesn't know about the ** operator in python.
>
>
> It appears that no serious python coders were consulted when writing
> these questions.
>
> ~Andrew
>

So that narrows the search for the culprit down to our Greek aquaintance? :)

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


#58611

FromChris Angelico <rosuav@gmail.com>
Date2013-11-07 11:59 +1100
Message-ID<mailman.2106.1383785963.18130.python-list@python.org>
In reply to#58601
On Thu, Nov 7, 2013 at 11:00 AM, Nathaniel Sokoll-Ward
<nathanielsokollward@gmail.com> wrote:
> Thought this group would appreciate this: www.metabright.com/challenges/python
>
> MetaBright makes skill assessments to measure how talented people are at different skills. And recruiters use MetaBright to find outrageously skilled job candidates.
>
> Python is a new area of expertise for us. We make "Challenges" for a bunch of languages and we're excited to finally have Python released. Give it a shot -- I'd love to hear what you think.

"""How could you open a file c:\scores.dat to write in binary?

outfile = open("c:\\scores.dat", "w")
outfile = open("c:\scores.dat", "a")
outfile = open("c:\\scores.dat", "w")
outfile = open("c:\\scores.dat", "wb")"""

Not technically wrong, but stylistically suspect; I would recommend
using forward slashes (which work fine on Windows) and avoiding the
drive letter, both of which avoid making your example
Windows-specific. (At least, I don't think there are any other
platforms Python supports that use drive letters; OS/2 support was
dropped a little while ago, though I believe Paul Smedley still
maintains a port. But I digress.)

"""Which method will write a pickled representation of the object to
an open file?"""

Method names without object names aren't all that useful. Do you mean
"Which method of the pickle module..."?

"""From which languages are Python classes derived from?"""

Sounds like Python history trivia more than a coding challenge, but if
that's what you want to go for, sure.

ChrisA

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


#58612

FromChris Angelico <rosuav@gmail.com>
Date2013-11-07 12:04 +1100
Message-ID<mailman.2107.1383786254.18130.python-list@python.org>
In reply to#58601
On Thu, Nov 7, 2013 at 11:00 AM, Nathaniel Sokoll-Ward
<nathanielsokollward@gmail.com> wrote:
> Thought this group would appreciate this: www.metabright.com/challenges/python
>
> MetaBright makes skill assessments to measure how talented people are at different skills. And recruiters use MetaBright to find outrageously skilled job candidates.
>
> Python is a new area of expertise for us. We make "Challenges" for a bunch of languages and we're excited to finally have Python released. Give it a shot -- I'd love to hear what you think.

By the way, here's a fairly bad solution to your final question:

array666=lambda x:b"\6\6\6" in bytes(x)

Works for the given test-cases! Doesn't work with arrays at all,
despite the description.

ChrisA

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


#58616

FromJohn Nagle <nagle@animats.com>
Date2013-11-06 17:31 -0800
Message-ID<l5eqi0$3ha$1@dont-email.me>
In reply to#58612
On 11/6/2013 5:04 PM, Chris Angelico wrote:
> On Thu, Nov 7, 2013 at 11:00 AM, Nathaniel Sokoll-Ward
> <nathanielsokollward@gmail.com> wrote:
>> Thought this group would appreciate this: www.metabright.com/challenges/python
>>
>> MetaBright makes skill assessments to measure how talented people are at different skills. And recruiters use MetaBright to find outrageously skilled job candidates.

   With tracking cookies blocked, you get 0 points.

				John Nagle

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


#58622

FromTim Chase <python.list@tim.thechases.com>
Date2013-11-06 21:19 -0600
Message-ID<mailman.2114.1383794270.18130.python-list@python.org>
In reply to#58616
On 2013-11-06 17:31, John Nagle wrote:
> >> MetaBright makes skill assessments to measure how talented
> >> people are at different skills. And recruiters use MetaBright to
> >> find outrageously skilled job candidates.  
> 
> With tracking cookies blocked, you get 0 points.

And with JavaScript blocked, you get bupkis. :-)

I was amused that the sidebar of similar challenges suggested that
the Python challenge might be similar to this one.  Ya think?  So
similar that even the URL is the same...

-tkc


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


#58687

FromNathaniel Sokoll-Ward <nathanielsokollward@gmail.com>
Date2013-11-07 10:38 -0800
Message-ID<c6a4b69c-51ee-4367-8bf0-aefed08d7bb1@googlegroups.com>
In reply to#58622
Wow! Thanks for all the feedback everyone. This content is fresh so I appreciate everyone's comments. As opposed to responding to each post individually, I'll just lump everything in here...

Andrew, big thanks for your comments:

> "What is the correct number of spaces for indentation in Python?" 
>
> I presume the question should be more along the lines of "What does PEP8 
> say?", because all answers are correct. 

I agree. Question has been edited.

>
> "String literals are written with what?" 
>
> The answer is not "ALl of these answers are correct" 

I believe that string literals can be written with single, double, or triple quotes: http://docs.python.org/release/2.5.2/ref/strings.html

> "Which is a correct way to perform exponentiation in Python?" 

This was a silly error. Thanks for pointing it out.

> What does the following code do? 
> def a(b, c, d): pass 
>
> My answer: "Defines a function which returns None", but that isn't one 
> of the choices. 

Roy, thanks for your note. When I run this code, the function just gets defined and nothing happens. None isn't returned. Do you recall why you found the options available to you unsuitable?

> """How could you open a file c:\scores.dat to write in binary? 
>
> outfile = open("c:\\scores.dat", "w") 
> outfile = open("c:\scores.dat", "a") 
> outfile = open("c:\\scores.dat", "w") 
> outfile = open("c:\\scores.dat", "wb")""" 
>
> Not technically wrong, but stylistically suspect; I would recommend 
> using forward slashes (which work fine on Windows) and avoiding the 
> drive letter, both of which avoid making your example 
> Windows-specific. (At least, I don't think there are any other 
> platforms Python supports that use drive letters; OS/2 support was 
> dropped a little while ago, though I believe Paul Smedley still 
> maintains a port. But I digress.) 

Excellent suggestion. We've gone ahead and made the change.

>
> """Which method will write a pickled representation of the object to 
> an open file?""" 
>
> Method names without object names aren't all that useful. Do you mean 
> "Which method of the pickle module..."? 

Again, great suggestion.

> 
> """From which languages are Python classes derived from?""" 
>
> Sounds like Python history trivia more than a coding challenge, but if 
> that's what you want to go for, sure. 

I agree it's not directly coding related. Our questions are actually sorted into topic buckets. We try to get a reading on people's knowledge in a bunch of different areas of a given skill. Familiarity with general knowledge facts such as this, gives us another data point to help parse out the types of questions the best developers tend to get right.

> By the way, here's a fairly bad solution to your final question: 
>
> array666=lambda x:b"\6\6\6" in bytes(x) 
>
> Works for the given test-cases! Doesn't work with arrays at all, 
> despite the description. 

Chris, I actually really like your answer, even if it doesn't satisfy the goal in the question. I'd give it a vote for cleverness!

> You know, I didn't even notice that. But since that was copied and 
> pasted, I would say that yes, it really does. That's a pretty simple 
> grammatical bugfix though. 

Silly error. Fixed.

> I have to concur with what several other people are saying here.  Several of MetaBright's questions are > ambiguously worded, or expect non-idiomatic Python code.  It might be helpful for you to ask (hire?) > some seasoned Python programmers to critique your questions. 

Thanks for the thoughts, John. I'd be lying if I said I wasn't disappointed with how many errors everyone here is picking out. Some of our Challenges are built exclusively by our users, others are built by someone who helped build MetaBright, while others, like the Python Challenge, are built with the help of contractors. Even so, the responsibility to make sure we are publishing high quality content falls on our shoulders and I regret we didn't go a better job of vetting this material. 

> With tracking cookies blocked, you get 0 points. 

> And with JavaScript blocked, you get bupkis. :-) 

I know that's frustrating. Our tech lead will be on here later today to explain why we do this.

Thanks again everyone!

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


#58689

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-07 18:48 +0000
Message-ID<mailman.2150.1383850141.18130.python-list@python.org>
In reply to#58687
On 07/11/2013 18:38, Nathaniel Sokoll-Ward wrote:
>>
>> My answer: "Defines a function which returns None", but that isn't one
>> of the choices.
>
> Roy, thanks for your note. When I run this code, the function just gets defined and nothing happens. None isn't returned. Do you recall why you found the options available to you unsuitable?
>

 >>> def a(b, c, d): pass
...
 >>> x=a(1,2,3);type(x)
<class 'NoneType'>

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

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


#58692

FromAlister <alister.ware@ntlworld.com>
Date2013-11-07 19:08 +0000
Message-ID<pyReu.25286$ql7.11998@fx33.am4>
In reply to#58687
On Thu, 07 Nov 2013 10:38:40 -0800, Nathaniel Sokoll-Ward wrote:

> Wow! Thanks for all the feedback everyone. This content is fresh so I
> appreciate everyone's comments. As opposed to responding to each post
> individually, I'll just lump everything in here...
> 
> 
>>
>> My answer: "Defines a function which returns None", but that isn't one
>> of the choices.
> 
> Roy, thanks for your note. When I run this code, the function just gets
> defined and nothing happens. None isn't returned. Do you recall why you
> found the options available to you unsuitable?

your sites answer is " defines a function that does nothing"
once you have defined the function try print (a(1,2,3))
you will see that is does indeed return none, as do all functions without 
an explicit return.
> 

> Thanks again everyone!
-- 
While you recently had your problems on the run, they've regrouped and
are making another attack.

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


#58721

FromRoy Smith <roy@panix.com>
Date2013-11-07 20:02 -0500
Message-ID<roy-E0C307.20020107112013@news.panix.com>
In reply to#58692
In article <pyReu.25286$ql7.11998@fx33.am4>,
 Alister <alister.ware@ntlworld.com> wrote:

> your sites answer is " defines a function that does nothing"
> once you have defined the function try print (a(1,2,3))
> you will see that is does indeed return none, as do all functions without 
> an explicit return.

Well, if you want to be truly pedantic about it (*), this defines a 
function without an explicit return and which does not return None:

def foo():
   raise Exception

and, for that matter:

def bar():
   import os
   os._exit(0)  # Or variations, such as exec()

(*) and I do.

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


#58725

Fromalex23 <wuwei23@gmail.com>
Date2013-11-08 11:49 +1000
Message-ID<l5hfuj$m2n$1@dont-email.me>
In reply to#58721
On 8/11/2013 11:02 AM, Roy Smith wrote:
> Well, if you want to be truly pedantic about it (*), this defines a
> function without an explicit return and which does not return None:
>
> def foo():
>     raise Exception


In [2]: import dis
In [3]: dis.dis(foo)
   2           0 LOAD_GLOBAL              0 (Exception)
               3 RAISE_VARARGS            1
               6 LOAD_CONST               0 (None)
               9 RETURN_VALUE

Seeing as we're being pedantic, the function *does* return None, it's 
just that the return value is never seen because an exception is raise.

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


#58726

FromRoy Smith <roy@panix.com>
Date2013-11-07 20:54 -0500
Message-ID<roy-187962.20540507112013@news.panix.com>
In reply to#58725
In article <l5hfuj$m2n$1@dont-email.me>, alex23 <wuwei23@gmail.com> 
wrote:

> On 8/11/2013 11:02 AM, Roy Smith wrote:
> > Well, if you want to be truly pedantic about it (*), this defines a
> > function without an explicit return and which does not return None:
> >
> > def foo():
> >     raise Exception
> 
> 
> In [2]: import dis
> In [3]: dis.dis(foo)
>    2           0 LOAD_GLOBAL              0 (Exception)
>                3 RAISE_VARARGS            1
>                6 LOAD_CONST               0 (None)
>                9 RETURN_VALUE
> 
> Seeing as we're being pedantic, the function *does* return None, it's 
> just that the return value is never seen because an exception is raise.

Dead code doesn't count.

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


#58728

Fromalex23 <wuwei23@gmail.com>
Date2013-11-08 12:08 +1000
Message-ID<l5hh32$qf4$1@dont-email.me>
In reply to#58726
On 8/11/2013 11:54 AM, Roy Smith wrote:
> Dead code doesn't count.

Neither do shifting goalposts.

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


#58731

FromRoy Smith <roy@panix.com>
Date2013-11-07 21:18 -0500
Message-ID<roy-11E1E6.21185807112013@news.panix.com>
In reply to#58728
In article <l5hh32$qf4$1@dont-email.me>, alex23 <wuwei23@gmail.com> 
wrote:

> On 8/11/2013 11:54 AM, Roy Smith wrote:
> > Dead code doesn't count.
> 
> Neither do shifting goalposts.

It's not a shifting goalpost.  My original statement was that:

def foo():
   raise Exception

defines a function which 1) has no explicit return statement and 2) does 
not return None.  I stand by that statement.  There is no possible 
codepath, no possible calling sequence, no possible execution 
environment, which will cause that function to return None.  That fact 
that one particular Python implementation happens to produce unreachable 
bytecode for returning None is meaningless.  Would you say that:

def baz():
   return None
   print "I got here"

is a function which prints "I got here"?

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


#58734

FromChris Angelico <rosuav@gmail.com>
Date2013-11-08 13:26 +1100
Message-ID<mailman.2173.1383877585.18130.python-list@python.org>
In reply to#58731
On Fri, Nov 8, 2013 at 1:18 PM, Roy Smith <roy@panix.com> wrote:
> It's not a shifting goalpost.  My original statement was that:
>
> def foo():
>    raise Exception
>
> defines a function which 1) has no explicit return statement and 2) does
> not return None.  I stand by that statement.  There is no possible
> codepath, no possible calling sequence, no possible execution
> environment, which will cause that function to return None.  That fact
> that one particular Python implementation happens to produce unreachable
> bytecode for returning None is meaningless.  Would you say that:
>
> def baz():
>    return None
>    print "I got here"
>
> is a function which prints "I got here"?

Granted, but I would describe this:

def foo(x):
    return "Hello, world!\n" + str(x)

as a function which returns a string. Is it? Well, not if str raises
an exception. Even if the only arguments you can give to foo will
result in exceptions, I would still say that, per design, this is a
function that returns a string. The possibility of raising an
exception (and thus not returning anything) doesn't change a
function's return type (by which I mean more than just what C would
use as the declaration - I could just as well say "Returns the name of
an employee", and the same argument would apply).

ChrisA

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


#58746

FromTim Chase <python.list@tim.thechases.com>
Date2013-11-07 22:05 -0600
Message-ID<mailman.2182.1383883428.18130.python-list@python.org>
In reply to#58731
On 2013-11-07 21:18, Roy Smith wrote:
> It's not a shifting goalpost.  My original statement was that:
> 
> def foo():
>    raise Exception
> 
> defines a function which 1) has no explicit return statement and 2)
> does not return None.  I stand by that statement.  There is no
> possible codepath, no possible calling sequence, no possible
> execution environment, which will cause that function to return
> None.

Well, for varying definitions of "that function", you can do

>>> def unerr(fn):
...     def wrapper(*args, **kwargs):
...             try:
...                     fn(*args, **kwargs)
...             except:
...                     return None
...     return wrapper
... 
>>> @unerr
... def foo():
...     raise Exception
... 
>>> print foo()
None

;-)

Beyond that, I'm sure one could resort to bytecode hacking to have
"that function" skip the raise...

-tkc


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


#58752

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-11-08 05:26 +0000
Message-ID<527c75fa$0$29983$c3e8da3$5496439d@news.astraweb.com>
In reply to#58746
On Thu, 07 Nov 2013 22:05:14 -0600, Tim Chase wrote:

> On 2013-11-07 21:18, Roy Smith wrote:
>> It's not a shifting goalpost.  My original statement was that:
>> 
>> def foo():
>>    raise Exception
>> 
>> defines a function which 1) has no explicit return statement and 2)
>> does not return None.  I stand by that statement.  There is no possible
>> codepath, no possible calling sequence, no possible execution
>> environment, which will cause that function to return None.
> 
> Well, for varying definitions of "that function", you can do
[snip modified functions]
> Beyond that, I'm sure one could resort to bytecode hacking to have "that
> function" skip the raise...

Now who's shifting the goalposts? Whether you edit the function's source 
code, wrap it in a decorator, or hack it's byte-code, it's not the same 
function as the one Roy showed above by any reasonable definition of "the 
same". As an intellectual exercise of how one might subvert the standard 
semantics of the Python compiler, it is interesting to consider (say) 
byte-code hacks that turn this source code:

def foo():
    raise Exception

into something that returns None, but by the same logic one might say 
that this function:

def spam():
    return 42

connects to some database over the Internet and deletes any table 
containing more than seven records. If we're going to allow those sorts 
of debating shenanigans, the obvious counter is "yes, but when I said 
that the function doesn't return None, I actually meant that it doesn't 
solve the Halting Problem, and it still doesn't do that, so I win nyah 
nyah nyah".



-- 
Steven

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


Page 1 of 2  [1] 2  Next page →

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


csiph-web