473,388 Members | 1,198 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

* operator--as in *args?

Hi,

I can't find any documentation for the * operator when applied in
front of a name. Is it a pointer?

What about the @ operator?

Are there python names for these operators that would make searching
for documentation on them more fruitful?

Thanks

Mar 18 '07 #1
7 10402
On Mar 18, 3:53 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
Hi,

I can't find any documentation for the * operator when applied in
front of a name. Is it a pointer?

What about the @ operator?

Are there python names for these operators that would make searching
for documentation on them more fruitful?

Thanks
For the *star, see apply here:
http://docs.python.org/lib/non-essen...-in-funcs.html

For example:
aFunction(*(1,2,3))
is equivalent to:
aFunction(1,2,3)

For the @ symbol, I assume you mean function decorators:
http://docs.python.org/ref/function.html

For example:
@someDecorator
def someFunc(): pass
is equivalent to:
def someFunc(): pass
someFunc = someDecorator(someFunc)

Mar 18 '07 #2
On Mar 18, 3:40 pm, "Dustan" <DustanGro...@gmail.comwrote:
For example:
aFunction(*(1,2,3))
is equivalent to:
aFunction(1,2,3)
That's the context I've seen it in, but written like this:

someFunc(*args)

I played around with it a little bit, and it appears the * operator
unpacks a list, tuple, or dictionary so that each element of the
container gets assigned to a different parameter variable. Although
with a dictionary, only the keys appear to be assigned to the
parameter variables, e.g.:

def g(a,b,c):
print a, b, c

dict = {"x":10, "y":20, "z":30}
g(*dict)

Is that right?

Mar 19 '07 #3
7stud <bb**********@yahoo.comwrote:
On Mar 18, 3:40 pm, "Dustan" <DustanGro...@gmail.comwrote:
For example:
aFunction(*(1,2,3))
is equivalent to:
aFunction(1,2,3)

That's the context I've seen it in, but written like this:

someFunc(*args)

I played around with it a little bit, and it appears the * operator
unpacks a list, tuple, or dictionary so that each element of the
container gets assigned to a different parameter variable. Although
with a dictionary, only the keys appear to be assigned to the
parameter variables, e.g.:

def g(a,b,c):
print a, b, c

dict = {"x":10, "y":20, "z":30}
g(*dict)

Is that right?
As far as it goes, yes. More generally, with any iterable x, the *x
construct in function call will pass as positional arguments exactly
those items which (e.g.) would be printed by the loop:
for item in x: print x

[[this applies to iterators, generators, genexps, and any other iterable
you may care to name -- not just lists, tuples, dicts, but also sets,
files open for reading [the items are the lines], etc, etc]].
Alex
Mar 19 '07 #4
En Sun, 18 Mar 2007 18:40:32 -0300, Dustan <Du**********@gmail.com>
escribió:
>I can't find any documentation for the * operator when applied in
front of a name. Is it a pointer?

For the *star, see apply here:
http://docs.python.org/lib/non-essen...-in-funcs.html
Also, you can read this section on the Tutorial:
http://docs.python.org/tut/node6.htm...00000000000000
or the more technical Language Reference:
http://docs.python.org/ref/calls.html and
http://docs.python.org/ref/function.html
>Are there python names for these operators that would make searching
for documentation on them more fruitful?
Uhhhm, none that I can think of :(

--
Gabriel Genellina

Mar 19 '07 #5
En Sun, 18 Mar 2007 22:21:41 -0300, Alex Martelli <al***@mac.comescribió:
7stud <bb**********@yahoo.comwrote:
>I played around with it a little bit, and it appears the * operator
unpacks a list, tuple, or dictionary so that each element of the
container gets assigned to a different parameter variable. Although
with a dictionary, only the keys appear to be assigned to the
parameter variables, e.g.:

def g(a,b,c):
print a, b, c

dict = {"x":10, "y":20, "z":30}
g(*dict)

Is that right?

As far as it goes, yes. More generally, with any iterable x, the *x
construct in function call will pass as positional arguments exactly
those items which (e.g.) would be printed by the loop:
for item in x: print x

[[this applies to iterators, generators, genexps, and any other iterable
you may care to name -- not just lists, tuples, dicts, but also sets,
files open for reading [the items are the lines], etc, etc]].
But the language reference says "sequence", not "iterable"
(http://docs.python.org/ref/calls.html) and a dictionary is not a
sequence. With Python 2.1 it was an error; it is not with 2.3 (I can't
test with 2.2 right now)

Python 2.1.3 (#35, Apr 8 2002, 17:47:50) [MSC 32 bit (Intel)] on win32
Type "copyright", "credits" or "license" for more information.
>>def f(*args, **kw):
.... print "args",args
.... print "kw",kw
....
>>d = {"a":1, "b":2, "c":3}
f(**d)
args ()
kw {'b': 2, 'c': 3, 'a': 1}
>>f(*d)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: f() argument after * must be a sequence

If allowing f(*d) is actually the intended behavior, maybe the wording in
the reference should be updated. If not, f(*d) should still raise an error.

--
Gabriel Genellina

Mar 19 '07 #6
Gabriel Genellina <ga*******@yahoo.com.arwrote:
...
As far as it goes, yes. More generally, with any iterable x, the *x
construct in function call will pass as positional arguments exactly
those items which (e.g.) would be printed by the loop:
for item in x: print x

[[this applies to iterators, generators, genexps, and any other iterable
you may care to name -- not just lists, tuples, dicts, but also sets,
files open for reading [the items are the lines], etc, etc]].

But the language reference says "sequence", not "iterable"
Yes, Python's docs often say "sequence" where they in fact mean
"iterable". I count (on all the .tex files under Doc/ in a current SVN
tree) 854 occurrences of "sequence" versus 251 occurrences of
"iterable", and I suspect the correct ratio would be rather close to the
reverse:-).
If allowing f(*d) is actually the intended behavior, maybe the wording in
the reference should be updated. If not, f(*d) should still raise an error.
Patches to the docs will doubtlessly be welcome (though fixing 1 out of
a suspected 600 or so misuses won't make a huge difference, it's still
definitely better than nothing:-).
Alex
Mar 19 '07 #7
On Mar 18, 7:52 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
def f(*args, **kw):

... print "args",args
... print "kw",kw
...>>d = {"a":1, "b":2, "c":3}
>f(**d)
Whoa! **? And applied to a function parameter? Back to the drawing
board.

On Mar 18, 7:21 pm, a...@mac.com (Alex Martelli) wrote:
More generally, with any iterable x, the *x
construct in function call will pass as positional arguments exactly
those items which (e.g.) would be printed by the loop:
for item in x: print x
Thanks.

On Mar 18, 7:52 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
def f(*args, **kw):

... print "args",args
... print "kw",kw
...>>d = {"a":1, "b":2, "c":3}
>f(**d)
Whoa! **? And applied to a function parameter name? Back to the
drawing board.

The following is what I discovered for anyone else that is interested:

More on Defining Functions(GvR tutorial, section 4.7)
--Keyword Argument Lists
--Arbitrary Argument Lists
--Unpacking Argument Lists

1) Applying ** to a function parameter:

def g(a, b, **xtraArgs):
print xtraArgs
g(b=20, c="big", a="hello", d=10)

#output: {'c': 'big', 'd': 10}

**xtraArgs will be a dictionary of all arguments sent to the function
with the syntax "keyword=value" if the keyword does not match any of
the function parameter names.

2)Applying * to a function parameter:

def g(a, *xtraArgs):
print xtraArgs
g(10, 20, 30, 40)

#output: (20, 30, 40)

*xtraArgs will be a tuple containing all arguments sent to the
function with the syntax "value" that do not match any function
parameter.

3) A function parameter with the syntax *xtraArgs1 must come before a
function parameter with the syntax **xtraArgs2:

def g(a, b, *xtraArgs1, **xtraArgs2):
print xtraArgs1
print xtraArgs2

g(10, 20, 30, 35, d=40, e="hello")

#(30, 35)
#{'e': 'hello', 'd': 40}

4) Using * to unpack a list, tuple, etc.(any iterable collection--see
earlier posts):

def g(a, b, c):
print a,b,c

tpl = (1, 2, 3)
g(*tpl)

#output: 1 2 3

5) Using ** to unpack dictionaries:

def g(a, b, c):
print a,b,c

dict = {"b":"world", "c":"goodbye", "a":"hello"}
g(**dict)

#output: hello world goodbye

Mar 19 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a...
2
by: Jim Jewett | last post by:
Normally, I expect a subclass to act in a manner consistent with its Base classes. In particular, I don't expect to *lose* any functionality, unless that was the whole point of the subclass. ...
6
by: Peter Kleiweg | last post by:
I'm still new to Python. All my experience with OO programming is in a distant past with C++. Now I have written my first class in Python. The class behaves exactly as I want, but I would like to...
2
by: Pierre Fortin | last post by:
This quest for understanding started very innocently... A simple error on my part, passing on args as "args" instead of "*args" to os.path.join() led me to wonder why an error wasn't raised... ...
15
by: Stefan Behnel | last post by:
Hi! I'm trying to do this in Py2.4b1: ------------------------------- import logging values = {'test':'bla'} logging.log(logging.FATAL, 'Test is %(test)s', values)...
10
by: Steven Bethard | last post by:
So, as I understand it, in Python 3000, zip will basically be replaced with izip, meaning that instead of returning a list, it will return an iterator. This is great for situations like: zip(*)...
1
by: Andreas Beyer | last post by:
Hi, If an exception gets raised while I am parsing an input file I would like to know where (in which line) the error occured. I do not want to create my own exception class for that purpose and...
27
by: TheDD | last post by:
Hello all, right now, i'm using the following macro to automatically add informations to exceptions: #define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args) but AFAIK, it's...
18
by: Joel Hedlund | last post by:
Hi! The question of type checking/enforcing has bothered me for a while, and since this newsgroup has a wealth of competence subscribed to it, I figured this would be a great way of learning...
5
by: Guillermo | last post by:
Hi, This must be very basic, but how'd you pass the same *args several levels deep? def func2(*args) print args # ((1, 2, 3),) # i want this to output (1, 2, 3) as func1!
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.