5 kyu

Function Overloading for Types

Description
Loading description...
Decorator
Language Features
Metaprogramming
  • Please sign in or sign up to leave a comment.
  • MrCookies78 Avatar

    This comment has been hidden.

  • Tsubasanut Avatar

    Python newbie here. Very nice kata, helped me with understanding decorators.

    My solution fails both recursive test with KeyError exception. I've tried locally to feed factorial function to my solution with no problem. Can I have a code for "rec_test" function so I can test is locally? Or some hint on how to debug it?

  • Expurple Avatar

    Love katas like this, Python always amazes me with its flexibility

  • dan_lazarescu Avatar

    My code fails at the following test: Methods can access 'self' var, even when not named 'self'.

    Traceback (most recent call last): File "/workspace/default/src/codewars-test/codewars_test/test_framework.py", line 111, in wrapper func() File "tests.py", line 212, in access_test test.assert_equals(the_ob.self_access('World'), 'World2')

    I'm not sure what else to verify...can you be more specific about the test case?

  • Kacarott Avatar

    Based on the feedback, it seems people generally like the concept of the Kata but I have a big problem with test coverage, not being clear with what is expected, and the sample tests not being representative enough.

    I'm gonna leave this up for the moment while I rewrited the Kata to be a lot more thorough, then I will rerelease it. (Since the added thoroughness and requirements I want to add will likely break every existing solution, and will essentially be a different kata.)

    Of course feel free to keep leaving feedback, just know this kata will be replaced (hopefully soon) with a better (harder?) version, that actually works well.

  • Awesome A.D. Avatar

    Good Kata! Decorators still do my head in, although they are conceptually so simple, but once inside it's hard to wrap (ha!) my head around where what why is going on. That said, I was torn between rating this as 4 or 3 kyu. In the end I went with 3 just because of the requirement that it work on bound methods. My solution passes but it is so simplistic I gotta suspect that there's something the tests aren't quite covering. I literally just check for the existence of a parameter named "self". Granted this would work on 99% of code out there, but maybe some programmer transitioning from another language prefers to write "this" instead...

  • Blind4Basics Avatar

    ok, so: there are things that aren't described properly and should be put in the sample tests too.

    
    @overload(int, int)
    def mult(x, y):
        return x*y
    
    @overload(str, int)
    def mult(x, y):
        return len(x)*y
    
    class Test_Ob:
        @overload(int, int)
        def mult(self, x, y):
            return x*y
        
        @overload(str, int)
        def mult(self, x, y):
            return len(x)*y
    
    the_ob = Test_Ob()
    
    • the part above has to be tested and described loud and clear (hence, sample tests and description). Because this means that we have to track two different mult(int,int) functions. So far, nothing of that kind can be expected from the description, and that's what totally tripped me up. (I was going for __name__ instead of something more appropriate...)
    • the random tests are done in a very different way than the fixed tests: the random section is the only place where you use varargs. => needs to be used/described too (again, description+sample tests)

    cheers

  • natan Avatar

    Wait. You want us to dispatch on function NAME as well? Or do you want us to figure out which scope they're in, to dispatch by scope -> name -> types ? What about lambdas? No, they cannot be used with decorator syntax, but they can be decorated:

    from functools import lru_cache
    
    f = lru_cache(lambda x: x + 1)
    

    the pypi package multipledispatch is arguably broken:

    # $ pip install --user multipledispatch
    
    from multipledispatch import dispatch
    
    @dispatch(int)
    def f(x):
        print(f'global f received {x!r}')
    
    def scope():
        @dispatch(int)
        def f(x):
            print(f'scoped f received {x!r}')
        return f
    
    g = scope()
    
    >>> g(5)  # good. behaves as expected
    scoped f received 5
    >>> f(5)  # very bad. calls wrong function
    scoped f received 5
    

    I was looking at this library to tell you how they solved it, expecting to see them use registration like functools.singledispatch or clever inspection to figure out the scope. But nah, it's broken. xD

    I marked this as a question (and I haven't even solved it) so I guess I should be asking something. Should there perhaps be some mention or acknowlegement of this? Maybe at least be mentioning that yes, it should be dispatching on function name? Seeing as how shady that is it should be supported and blessed by the description I think, instead of having solvers go out on a limb.

    Either acknowledge and bless it, or do like functools.singledispatch IMO and from what I can tell to be reasonable.

    Or maybe I misunderstand and there's a reasonable way to do it.

  • Blind4Basics Avatar

    Hi,

    So far, so good, but you need to put more things into the sample tests. Right now, I'm failing on Returns correct values and Can be used in lambdas and I'll be forced to poke in the dark to get what the problem is because I don't know the definitions/context used.

  • FArekkusu Avatar
    Test 39
      3031 should equal 3374
      Test Passed
      Test Passed
    

    Sometimes tests randomly fail, probably due to collisions in the input types.

  • Blind4Basics Avatar

    https://www.codewars.com/kata/5f24315eff32c4002efcfc6a ?

    yeah, sounds like a somewhat duplicate, doesn't it?

  • FArekkusu Avatar

    Tests should use it blocks.

  • user9644768 Avatar

    Sample tests are broken.

  • mauro-1 Avatar
  • Kacarott Avatar

    Please let me know any tests I have forgotten, and any issues of course. I am also very uncertain about the difficulty level so please tell me what you think about it.