1

Is there a python method equivalent to * and ** magic?

So, what I mean by this is instead of doing:

a = (1,2,3)
method(*a)

Instead do something like:

a = (1,2,3)
method(remove_magic(a))

Whilst reading through code I see * and ** and just wish there was a nicer more readable way of doing this. I am hoping there is something within the python standard library which does this for me.

tripleee
  • 175,061
  • 34
  • 275
  • 318
user204088
  • 1,805
  • 3
  • 17
  • 21
  • What do you think `*` and `**` are? What are you talking about? – Maroun Feb 23 '15 at 12:18
  • 4
    I have no idea what you are asking about here. Are you looking for a way to intercept calls using `*` and `**` argument expansion? If so there is none, because that has nothing to do with the object being called. – Martijn Pieters Feb 23 '15 at 12:18
  • 2
    If you are looking for the *operators* for multiplication and exponentiation, then there are special methods for that, but you need to be a lot clearer as to what problem you are trying to solve. – Martijn Pieters Feb 23 '15 at 12:19
  • @MartijnPieters I think you were right with your first guess, they probably don't have a firm grasp of argument expansion. – Cory Kramer Feb 23 '15 at 12:34
  • 2
    After your update: the `*` and `**` syntax results in different bytecode being produced by the compiler. There is no way to reproduce that with a 'magic function', no. I'd find using a different method to reproduce the same results *less readable* anyway, as you'd confuse experienced programmers. – Martijn Pieters Feb 23 '15 at 12:36
  • 4
    I guess you're looking for [`apply`](https://docs.python.org/2/library/functions.html#apply), but Python docs explicitly say splats are the preferred way. – georg Feb 23 '15 at 12:41
  • This might be helpful: < http://stackoverflow.com/questions/19526300/does-argument-unpacking-use-iteration-or-item-getting >. – ely Feb 23 '15 at 13:31

2 Answers2

2

YES, THERE IS AN EQUIVALENT

Technically, you can use the builtin apply() function. According to the python docs:

The use of apply() is equivalent to function(*args, **keywords).

so you can do apply(function, args, keywords=None). (keywords is optional.)

BUT DON'T USE IT

On top of being non-idiomatic and confusing to more experienced programmers, this function has been deprecated and should not be used in new code:

Deprecated since version 2.3: Use function(*args, **keywords) instead of apply(function, args, keywords)

Darrick Herwehe
  • 3,553
  • 1
  • 21
  • 30
0

There is no way to make a function that does the same thing as * or **. You'd have to modify method itself. The reason is that the remove_magic function could return a tuple/list at best, but you already have one:

a = (1,2,3)
method(remove_magic(a))

A pythonic alternative might be to unpack the tuple and to pass the arguments explicitly:

a = (1, 2, 3)
x, y, z = a
method(x, y, z)

In Python 2.x, you can use apply, but I don't find it more legible. Its also not available in python 3.

apply(method, a)

The official reccomendation is to simply stick with

method(*a)
jdm
  • 9,470
  • 12
  • 58
  • 110