Giter VIP home page Giter VIP logo

Comments (3)

gregmalcolm avatar gregmalcolm commented on July 17, 2024

Hi @mandaris.

While there is a sharp increase in difficulty during "about_none" it needs to be early in the koans as it covers fundamentals. I've always meant to put together a screencast and/or wiki page for this one, but just never got around to it.

I'm guessing the koan that is giving you problems is the 3rd one:
test_what_exception_do_you_get_when_calling_nonexistent_methods

To make more sense of it I recommend opening a python console (IDLE) and trying out some ideas there. The part that is confusing here is understanding what ex._class.__name do.

ex is an exception so it's not quite so easy to grab it and start playing with. Try playing with a piece of text (like
"hello") instead.

First off we need to know what class does. All python objects what are a type of class have a class attribute.

"hello".class gives us this:

$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> "Hello".__class__
<type 'str'>

So class apparently gives is information about the datatype (class) of the code on the left of the period.

However it returns <type 'str'> which isn't very convienent for use in an assertEquals statement:

>>> "Hello" == <type 'str'>
  File "<stdin>", line 1
    assert "Hello" == <type 'str'>
                      ^
SyntaxError: invalid syntax

This doesn't work because <type 'str'> is just a textual representation of "str"

>>> "Hello".__class__ == str                                                                                                                                                                                                                
True

So to make it more obvious we can call .name on the str to turn it into plain text:

>>> "Hello".__class__.__name__
'str'

So if there was an assert it would look like this:
self.assertEquals('str', "Hello".class.name)

Notice I have single quotes around 'str'? Thats vital.

So how does this apply to the koan?
Well currently the error you should be seeing when running the Koans is this:

You have not yet reached enlightenment ...
  AssertionError: '-=> FILL ME IN! <=-' != 'AttributeError'

The right hand side of the assert that evaluates ex.class.name is giving the string 'AttributeError'. Thats the class name of the Exception, turned into text. You just need to replace the "__" part of the assert with the same.

Hope that helps.

Additional hints:
A great number of students actually commit their answers to github. If you click on the Network button on the github page you can look through the graph, find a fork that has answers and see how another student is tacking the problem. For example:
https://github.com/Matt-Stevens/python_koans/blob/matts/python2/koans/about_none.py

In these circumstances its perfectly ok to be cheating as long as you're learning from the answers and not just blindly copying... :)

There is even an "answers" branch in the original python_koans. Only thing is it's a little out of date.

from python_koans.

mandaris avatar mandaris commented on July 17, 2024

Thank you, Greg!

That was exactly the one that I was having problems with. I started to panic and replaced the ex.class.name with ex.class.name and other things.

Bah!

After I see the answer, it becomes so obvious.

Of course…. it is afterwards…

Onward!

Valediction,
Mandaris Moore III

On Apr 29, 2013, at 4:51 AM, Greg Malcolm [email protected] wrote:

Hi @mandaris.

While there is a sharp increase in difficulty during "about_none" it needs to be early in the koans as it covers fundamentals. I've always meant to put together a screencast and/or wiki page for this one, but just never got around to it.

I'm guessing the koan that is giving you problems is the 3rd one:

test_what_exception_do_you_get_when_calling_nonexistent_methods

To make more sense of it I recommend opening a python console (IDLE) and trying out some ideas there. The part that is confusing here is understanding what ex._class.__name do.

ex is an exception so it's not quite so easy to grab it and start playing with. Try playing with a piece of text (like
"hello") instead.

First off we need to know what class does. All python objects what are a type of class have a class attribute.

"hello".class gives us this:

$ python
Python 2.7.2 (default, Jun 20 2012, 16:23:33)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

"Hello".class
<type 'str'>
So class apparently gives is information about the datatype (class) of the code on the left of the period.

However it returns which isn't very convienent for use in an assertEquals statement:

"Hello" == <type 'str'>
File "", line 1
assert "Hello" == <type 'str'>
^
SyntaxError: invalid syntax
This doesn't work because is just a textual representation of "str"

"Hello".class == str
True
So to make it more obvious we can call .name on the str to turn it into plain text:

"Hello".class.name
'str'
So if there was an assert it would look like this:
self.assertEquals('str', "Hello".class.name)

Notice I have single quotes around 'str'? Thats vital.

So how does this apply to the koan?
Well currently the error you should be seeing when running the Koans is this:

You have not yet reached enlightenment ...
AssertionError: '-=> FILL ME IN! <=-' != 'AttributeError'
The right hand side of the assert that evaluates ex.class.name is giving the string 'AttributeError'. Thats the class name of the Exception, turned into text. You just need to replace the "__" part of the assert with the same.

Hope that helps.

Additional hints:
A great number of students actually commit their answers to github. If you click on the Network button on the github page you can look through the graph, find a fork that has answers and see how another student is tacking the problem. For example:
https://github.com/Matt-Stevens/python_koans/blob/matts/python2/koans/about_none.py

In these circumstances its perfectly ok to be cheating as long as you're learning from the answers and not just blindly copying... :)

There is even an "answers" branch in the original python_koans. Only thing is it's a little out of date.


Reply to this email directly or view it on GitHub.

from python_koans.

gregmalcolm avatar gregmalcolm commented on July 17, 2024

NP!

from python_koans.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.