473,396 Members | 1,724 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,396 software developers and data experts.

Nested Parameter Definitions

I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>>def x ((p0, p1), p2):
.... return p0,p1,p2
....
>>x(('Does', 'this'), 'work')
('Does', 'this', 'work')
>>>
Ruben commented that there was a poll on this features continued
existence taken at PyCon and it could go.

Just as I found it, it could go

I wondered if those of you with some Python experience new of nested
parameters and don't use them; or just forgot/don't know it is
possible?

- Paddy.

Oh - the blog entry is at http://paddy3118.blogspot.com/2007/0...parameter.html

Feb 25 '07 #1
9 1512
On Feb 25, 1:00 pm, "Paddy" <paddy3...@googlemail.comwrote:
I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>def x ((p0, p1), p2):

... return p0,p1,p2
...>>x(('Does', 'this'), 'work')

('Does', 'this', 'work')

Ruben commented that there was a poll on this features continued
existence taken at PyCon and it could go.

Just as I found it, it could go

I wondered if those of you with some Python experience new of nested
parameters and don't use them; or just forgot/don't know it is
possible?

- Paddy.

Oh - the blog entry is athttp://paddy3118.blogspot.com/2007/02/pythons-function-nested-paramet...
I didn't know about it either. Without the call example, I would have
had a hard time to try to figure out what these extra brackets are
for. For this reason, I think that an explicit unpack is more
readable, and thus better.

Feb 25 '07 #2
On Feb 25, 6:00 pm, "Paddy" <paddy3...@googlemail.comwrote:
I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>def x ((p0, p1), p2):

... return p0,p1,p2
...>>x(('Does', 'this'), 'work')

('Does', 'this', 'work')
Reminds me of LeLisp! It had a similar feature. IIRC you could write
for example (I think 'df' was LeLisp for 'defun'):
(df mycar (a . b) a)
or
(df mylist L L)
or
(df mycaadr (a (b . c) . e) b)

I didn't know that this was possible in python and it does surprise
me. It feels at odd with the python philosophy.

--
Arnaud

Feb 25 '07 #3
On Feb 25, 7:06 pm, "Virgil Dupras" <hardcoded.softw...@gmail.com>
wrote:
On Feb 25, 1:00 pm, "Paddy" <paddy3...@googlemail.comwrote:
I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>>def x ((p0, p1), p2):
... return p0,p1,p2
...>>x(('Does', 'this'), 'work')
('Does', 'this', 'work')
Ruben commented that there was a poll on this features continued
existence taken at PyCon and it could go.
Just as I found it, it could go
I wondered if those of you with some Python experience new of nested
parameters and don't use them; or just forgot/don't know it is
possible?
- Paddy.
Oh - the blog entry is athttp://paddy3118.blogspot.com/2007/02/pythons-function-nested-paramet...

I didn't know about it either. Without the call example, I would have
had a hard time to try to figure out what these extra brackets are
for. For this reason, I think that an explicit unpack is more
readable, and thus better.
The following example shows three possible ways of accessing nested
data. i think , after now knowing how to use it, that the f0 function
with nested parameters and its subsequent use is the most readable.
Manual unpacking in the list comprehension for f1 is messy. No real
reason for liking f0 over f2 except its shiny!
>>def f0 ((p0, p1), p2):
.... pass
....
>>def f1 (p0, p1, p2):
.... pass
....
>>def f2(p):
.... (p0, p1), p2 = p
....
>>data = [ ((1, 2), 3), ((4, 5), 6)]
[ f0(*datum) for datum in data]
[None, None]
>>[ f1(datum[0][0], datum[0][1], datum[1]) for datum in data]
[None, None]
>>[ f2(datum) for datum in data]
[None, None]
>>>
- Paddy.

Feb 25 '07 #4
On Sun, 25 Feb 2007 10:00:31 -0800, Paddy wrote:
I wondered if those of you with some Python experience new of nested
parameters and don't use them; or just forgot/don't know it is
possible?
I learnt about this some time ago. I don't often use it, although it makes
sense to write this:

def parrot(x, (y, z)):
pass

instead of this:

def parrot(x, a2tuple):
y, z = a2tuple
pass

--
Steven D'Aprano

Feb 26 '07 #5
On Sun, 25 Feb 2007 11:06:03 -0800, Virgil Dupras wrote:
On Feb 25, 1:00 pm, "Paddy" <paddy3...@googlemail.comwrote:
>I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>>def x ((p0, p1), p2):

... return p0,p1,p2
[snip]
I didn't know about it either. Without the call example, I would have
had a hard time to try to figure out what these extra brackets are
for. For this reason, I think that an explicit unpack is more
readable, and thus better.
And now that you do know, it is not hard to figure it out at all.

Nested parameters are no harder to figure out than *args or arg=value.
It's something that you learn, once, and then it is easy forever.

To my mind, def x ((p0, p1), p2) _is_ an explicit unpack. It just takes
place in the parameter definition, where you can see it even if the
source code is not available, not in the body of the function code, where
you may or may not even be able to find it.

--
Steven D'Aprano

Feb 26 '07 #6
En Sun, 25 Feb 2007 15:00:31 -0300, Paddy <pa*******@googlemail.com>
escribió:
>>>def x ((p0, p1), p2):
... return p0,p1,p2
The first time I saw it used was in Zope, a long time ago. And I like it.
Of course it only has any sense if you expect the tuple (p0,p1) to exist
*before* the function is called; by example, when it's the return value of
some other function.

--
Gabriel Genellina

Feb 26 '07 #7
"Arnaud Delobelle" <ar*****@googlemail.comwrote:

On Feb 25, 6:00 pm, "Paddy" <paddy3...@googlemail.comwrote:
I blogged on finding a new-to-me feature of Python, in that you are
allowed to nnest parameter definitions:
>>def x ((p0, p1), p2):
... return p0,p1,p2
...>>x(('Does', 'this'), 'work')

('Does', 'this', 'work')

Reminds me of LeLisp! It had a similar feature. IIRC you could write
for example (I think 'df' was LeLisp for 'defun'):
(df mycar (a . b) a)
or
(df mylist L L)
or
(df mycaadr (a (b . c) . e) b)

I didn't know that this was possible in python and it does surprise
me. It feels at odd with the python philosophy.
Not at all - it much nicer than you think, and there is no "nesting"
involved - Penguins carry their eggs on their feet.

The original function definition describes a function that has a two element
tuple as a first parameter, and something else as a second one.
The first two names provide a means of accessing the elements of the
tuple, instead of using slicing.

look at this:
>>def f((a,b),c):
return a,b,c
>>tup = ('hi','there')
f(tup,'foo')
('hi', 'there', 'foo')
>>lis = ['hi','there']
f(lis,'foo')
('hi', 'there', 'foo')
>>d,e,f = f(lis,42)
print d,e,f
hi there 42
>>e
'there'
>>s = 'go'
f(s,'back')
('g', 'o', 'back')
>>>
Long live duck typing...

- Hendrik
Feb 26 '07 #8
Virgil Dupras:
Without the call example, I would have
had a hard time to try to figure out what these extra brackets are
for. For this reason, I think that an explicit unpack is more
readable, and thus better.
I can't agree.

Bye,
bearophile

Feb 26 '07 #9
On Feb 25, 11:41 pm, Dennis Lee Bieber <wlfr...@ix.netcom.comwrote:
Just looks like an extension of the normal tuple unpacking feature
of the language.
Yep, it looks like good Python to me too. Maybe the tutorial could be
extended to cover this form of parameter too?
It would be good if Brett Cannon could add some comments on why he
asked for a show of hands on the features use?
I'd hate for the feature to be removed because it is not often used,
when the reason it is not often used is because it is hard to find
examples of its use, because its buried in the language reference
manual.

Do any Python books mention nested parameters?

- Paddy.

Feb 26 '07 #10

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

Similar topics

6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
9
by: OKB (not okblacke) | last post by:
For a variety of reasons, I'm interested in putting together some code that will allow me to created structures out of nested classes, something like: class class1: def methA(self): print...
2
by: Forgone Conclusion | last post by:
Hi, I have a View that groups and sums up totals. This View is then nested within in another View and used (it needs to be done like this). What i need to do is to be able to vary the records...
3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
1
by: Yigit Ozgul | last post by:
Hi, I have a database where i keep 4 tables. the modules table: mdlID mdlName mdlstatus the submodules table:
1
by: nitrogenycs | last post by:
Hello, Code below. I've run into the following problem. I want to pass DCManagedTexture* as a template parameter into the DCDefaultResourceManager template. The DCManagedTexture class is...
2
by: Quinnie | last post by:
Hi, I have a homework assignment that I'm so confused and really need help with. Here's the description, any help would be appreciated. Thanks! Assume we have a statically-scoped language...
6
by: Carsten | last post by:
Hello Folks, I encountered a problem with SQL server 2000 and UDFs. I have a scalar UDF and a table UDF where I would like the scalar UDF to provide the argument for the table UDF like in: ...
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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...
0
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...
0
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,...

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.