473,545 Members | 2,388 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

* 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 10408
On Mar 18, 3:53 pm, "7stud" <bbxx789_0...@y ahoo.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(s omeFunc)

Mar 18 '07 #2
On Mar 18, 3:40 pm, "Dustan" <DustanGro...@g mail.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**********@y ahoo.comwrote:
On Mar 18, 3:40 pm, "Dustan" <DustanGro...@g mail.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**********@g mail.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.come scribió:
7stud <bb**********@y ahoo.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*******@yaho o.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.a r>
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.a r>
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=va lue" 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":"goodby e", "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
1848
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 handler supports. So, my mock class has to have appropriate methods defined on it. To avoid the hassle of manually defining lots of mock classes, I...
2
9625
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. (e.g., a security-restricted version, or an interface implementation that doesn't require a filesystem.) One (common?) exception seems to occur in...
6
2307
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 get comments about coding style. I'm especially unsure about how a class should be documented, what to put in, and where. When to use double quotes,...
2
2738
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... def foo(*args): ... return os.path.join(*args) foo('a','b') # returns 'a/b'
15
2960
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
2193
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(*) where I want to receive tuples of (item1, item2, item3) from the iterables. But it doesn't work well for a situation like: zip(*tuple_iter)
1
2276
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 I also do not want to deal with all possible kinds of exceptions that might occur. I came up with the following code: inp = file(file_name)
27
5898
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 gcc specific. Is there a way to do it in a standard c++ way? TIA
18
2730
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 from the experts. I feel there's a tradeoff between clear, easily readdable and extensible code on one side, and safe code providing early errors and...
5
1428
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
7487
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7420
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7680
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7446
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7778
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.