Code Monkey home page Code Monkey logo

Comments (9)

micheles avatar micheles commented on July 24, 2024

a is a positional argument, so the decorator module is doing the right thing. It looks like you are confusing positional arguments with a default with keyword arguments. Please have a look at the Python documentation.

from decorator.

altendky avatar altendky commented on July 24, 2024

I just ran into this as well. Comments about samplefn() below are considering it undecorated and discussing what the terms would be without decorator being involved. It appears that at least the start of the definitions are unchanged between 2.7 and 3.8.

On the calling side of the activity they are arguments.

https://docs.python.org/2.7/glossary.html#term-argument
https://docs.python.org/3.8/glossary.html#term-argument

keyword argument: an argument preceded by an identifier (e.g. name=) in a function call

positional argument: an argument that is not a keyword argument.

It seems that a in samplefn(1, a="askeyword") is a keyword argument. 1 there is a positional argument. In samplefn(1, "inline") the parameter a will be filled by by the positional argument "inline".

Now on the other side, the function not the call, they are parameters.

https://docs.python.org/2.7/glossary.html#term-parameter
https://docs.python.org/3.8/glossary.html#term-parameter

positional-or-keyword: specifies an argument that can be passed either positionally or as a keyword argument.

So in def samplefn(x, a="a1") both x and a are positional-or-keyword parameters.

As an extra piece, yes if you def f(*, x): then decorator will pass x always via kwargs.

It's not obvious to me yet whether for the default value case samplefn(1) should put "a1" into the positional or keyword arguments. At the moment I don't have any particular contention with decorator's present behavior of putting it in the positional arguments. It does seem that samplefn(1, "inline") should put "inline" in positional args, as it does presently. Per the documentation a="askeyword" in samplefn(1, a="askeyword") is a keyword argument and thus ought to go into the keyword arguments, which it presently does not.

Then again, perhaps you were referring to some other section of the docs which is in conflict with the glossary?

from decorator.

altendky avatar altendky commented on July 24, 2024

I'll note that 2.6 is much less clear about all this in the glossary. It doesn't even have an entry for 'parameter' and refers to a function having arguments in its definition.

https://docs.python.org/2.6/glossary.html#term-argument

A value passed to a function or method, assigned to a named local variable in the function body. A function or method may have both positional arguments and keyword arguments in its definition. Positional and keyword arguments may be variable-length: * accepts or passes (if in the function definition or call) several positional arguments in a list, while ** does the same for keyword arguments in a dictionary.

Any expression may be used within the argument list, and the evaluated value is passed to the local variable.

from decorator.

micheles avatar micheles commented on July 24, 2024

Per the documentation a="askeyword" in samplefn(1, a="askeyword") is a keyword argument and thus ought to go into the keyword arguments, which it presently does not.

I believe it is a bug in the Python documentation. The calling syntax means nothing, the distinction between positional and keyword arguments has to be done at the function definition.

from decorator.

altendky avatar altendky commented on July 24, 2024

I don't understand what the argument is for decorator.decorator provided *args, **kwargs to be different than those that the nominal wrapping decorator pattern results in.

https://repl.it/@altendky/VillainousTangibleObjectpool-2

import decorator


@decorator.decorator
def a(f, *args, **kwargs):
    print('a', args, kwargs)


def b(f):
    def wrapper(*args, **kwargs):
        print('b', args, kwargs)

    return wrapper


def f(a, b):
    pass


af = a(f)
bf = b(f)


af(1, 2)
af(1, b=2)
af(a=1, b=2)

print()

bf(1, 2)
bf(1, b=2)
bf(a=1, b=2)
a (1, 2) {}
a (1, 2) {}
a (1, 2) {}

b (1, 2) {}
b (1,) {'b': 2}
b () {'a': 1, 'b': 2}

from decorator.

altendky avatar altendky commented on July 24, 2024

For clarity, this appears to be fixed in the latest (py3-only) release. I did not dig into what commit fixed it. Thanks.

https://replit.com/@altendky/VillainousTangibleObjectpool-3

a (1, 2) {}
a (1,) {'b': 2}
a () {'a': 1, 'b': 2}

b (1, 2) {}
b (1,) {'b': 2}
b () {'a': 1, 'b': 2}

from decorator.

micheles avatar micheles commented on July 24, 2024

It is a by-product of a change in the implementation (now I am using the Signature object instead of exec). BTW, I regard the previous behavior to be correct (a and b are positional argument, not keyword arguments) and the new one to be wrong, but I have stopped fighting it.

from decorator.

micheles avatar micheles commented on July 24, 2024

The behavior that you wanted has been disabled by default because of backward compatibility issues (see the discussion in #104) but you can enable it by using the kwsyntax flag (see the docs).

from decorator.

altendky avatar altendky commented on July 24, 2024

Thanks for the extra effort to retain this feature. Also for the follow up here.

from decorator.

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.