Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-22 Thread shankha
Write code which is readable and then put in effort to speed things
up, if need be.

The "Code 1" is definitely confusing.  Why don't you create a class
and fill up the member variables. Much more readable.

There is a dis module which allows you to dump assembly code. You can
check how many instructions are being generated.
Thanks
Shankha Banerjee


On Fri, May 23, 2014 at 12:21 PM, Mandar Vaze / मंदार वझे
 wrote:
> Currently I came across the code that returned 9 values (return statement
> spanned 5 lines due to pep8 limitation of 79 characters per line)
>
> The function returns various values that are used by the template to render
> HTML
>
> To give you an idea - consider following two code snippets :
> (This is just a sample - it might lead you to believe these are various
> columns of a "user" table. Actual code isn't a row from DB table.
> All 9 values are computed, and aren't related to each other)
>
> Code 1:
> ...
> return dict(fname=fname, lname=lname, saluation=salutation,
>  gender=gender, addr1=addr1, addr2=addr2,
>  city=city, state=state, country=country)
>
> Code 2:
>
> user = {}
> user['fname'] = fname
> user['lname'] = lname
> ...
> ...
> ...
> user['country'] = country
>
> return dict(user=user)
>
>
> I was told first style is more "readable" since it tells you what is being
> passed to the template (I disagree, but it is subjective/personal opinion)
>
> Queries :
>
> 1. Is there a *limit* on "How many values can be returned from a function" ?
> 2. What is *performance impact* of returning so many values - (push and pop
> from the stack)
> 3. Based on #1 and #2 is there a *recommendation* on "where to draw the
> line"
>
> Thanks,
> -Mandar
> ___
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Rohit Chormale
How is it if you use DataContainer class  & set attributes of that class.
Something like,

class Data(object):

  def __init__(self, **kwargs):
object.__setattr__(self, 'attribs', kwargs)

  def __getattr__(self, item):
if item in self.attribs:
return self.attribs[item]
else:
raise AttributeError

  def __setattr__(self, key, value):
if key in self.attribs:
self.attribs[key] = value
else:
object.__setattr__(self, key, value)

So in future, if you return 10 or more values, it will not be problematic.

Though this reply doesn't give answer of your first 2 queries, it might be
useful to draw a line.

Regards
Rohit




On Fri, May 23, 2014 at 12:25 PM, shankha  wrote:

> Write code which is readable and then put in effort to speed things
> up, if need be.
>
> The "Code 1" is definitely confusing.  Why don't you create a class
> and fill up the member variables. Much more readable.
>
> There is a dis module which allows you to dump assembly code. You can
> check how many instructions are being generated.
> Thanks
> Shankha Banerjee
>
>
> On Fri, May 23, 2014 at 12:21 PM, Mandar Vaze / मंदार वझे
>  wrote:
> > Currently I came across the code that returned 9 values (return statement
> > spanned 5 lines due to pep8 limitation of 79 characters per line)
> >
> > The function returns various values that are used by the template to
> render
> > HTML
> >
> > To give you an idea - consider following two code snippets :
> > (This is just a sample - it might lead you to believe these are various
> > columns of a "user" table. Actual code isn't a row from DB table.
> > All 9 values are computed, and aren't related to each other)
> >
> > Code 1:
> > ...
> > return dict(fname=fname, lname=lname, saluation=salutation,
> >  gender=gender, addr1=addr1, addr2=addr2,
> >  city=city, state=state, country=country)
> >
> > Code 2:
> >
> > user = {}
> > user['fname'] = fname
> > user['lname'] = lname
> > ...
> > ...
> > ...
> > user['country'] = country
> >
> > return dict(user=user)
> >
> >
> > I was told first style is more "readable" since it tells you what is
> being
> > passed to the template (I disagree, but it is subjective/personal
> opinion)
> >
> > Queries :
> >
> > 1. Is there a *limit* on "How many values can be returned from a
> function" ?
> > 2. What is *performance impact* of returning so many values - (push and
> pop
> > from the stack)
> > 3. Based on #1 and #2 is there a *recommendation* on "where to draw the
> > line"
> >
> > Thanks,
> > -Mandar
> > ___
> > BangPypers mailing list
> > BangPypers@python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> ___
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Noufal Ibrahim KV
On Fri, May 23 2014, Rohit Chormale wrote:

> How is it if you use DataContainer class  & set attributes of that class.
> Something like,
>
> class Data(object):
>
>   def __init__(self, **kwargs):
> object.__setattr__(self, 'attribs', kwargs)
>
>   def __getattr__(self, item):
> if item in self.attribs:
> return self.attribs[item]
> else:
> raise AttributeError
>
>   def __setattr__(self, key, value):
> if key in self.attribs:
> self.attribs[key] = value
> else:
> object.__setattr__(self, key, value)

[...]

You could cheat by doing this.

class Data(object):
   def __init__(self, **kwargs):
  self.__dict__ = kwargs

instead of the above implementation. It's, of course, untested and
unverified.


-- 
Cordially,
Noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Noufal Ibrahim KV
On Fri, May 23 2014, Mandar Vaze / मंदार वझे wrote:

> Currently I came across the code that returned 9 values (return statement
> spanned 5 lines due to pep8 limitation of 79 characters per line)
>
> The function returns various values that are used by the template to render
> HTML
>
> To give you an idea - consider following two code snippets :
> (This is just a sample - it might lead you to believe these are various
> columns of a "user" table. Actual code isn't a row from DB table.
> All 9 values are computed, and aren't related to each other)

My usual way of doing this is something like


return dict(fname = fname,
lname = lname,
saluation = salutation,
gender= gender,
addr1 = addr1,
addr2 = addr2,
city  = city,
state = state,
country   = country)

I'm not sure what rules this violates but "it works for me" especially
when I read the code later.

Also, I'm not too sure if you should be worried, atleast on an abstract
level, about the number of keys this dictionary has. If they are all a
single logical coherent unit, it'll fit in the programmers
head. e.g. "an address" makes sense regardless of how many fields it has
(first name, last name, street, pin code etc.). If on the other hand
it's something vague that makes sense only to your own program, it'll be
harder to justify and might require some amount of cleanup.

I'm not sure about the performance impact. There are no "so many
values". You're returning just a (reference to a) single dictionary. The
values will be pulled out of the memory when you access them. 

I don't think this answers your question since it's mostly opinion but
nevertheless...


[...]


-- 
Cordially,
Noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread kracekumar ramaraju
You can use namedtuple.

from collections import namedtuple
Person = namedtuple('Person', ['foo', 'bar', 'baz'])
p = Person(foo='foo', bar='bar', baz='baz')

print p.foo
'foo'



On Fri, May 23, 2014 at 1:23 PM, Noufal Ibrahim KV
wrote:

> On Fri, May 23 2014, Rohit Chormale wrote:
>
> > How is it if you use DataContainer class  & set attributes of that class.
> > Something like,
> >
> > class Data(object):
> >
> >   def __init__(self, **kwargs):
> > object.__setattr__(self, 'attribs', kwargs)
> >
> >   def __getattr__(self, item):
> > if item in self.attribs:
> > return self.attribs[item]
> > else:
> > raise AttributeError
> >
> >   def __setattr__(self, key, value):
> > if key in self.attribs:
> > self.attribs[key] = value
> > else:
> > object.__setattr__(self, key, value)
>
> [...]
>
> You could cheat by doing this.
>
> class Data(object):
>def __init__(self, **kwargs):
>   self.__dict__ = kwargs
>
> instead of the above implementation. It's, of course, untested and
> unverified.
>
>
> --
> Cordially,
> Noufal
> http://nibrahim.net.in
> ___
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com *
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Noufal Ibrahim KV
On Fri, May 23 2014, kracekumar ramaraju wrote:

> You can use namedtuple.
>
> from collections import namedtuple
> Person = namedtuple('Person', ['foo', 'bar', 'baz'])
> p = Person(foo='foo', bar='bar', baz='baz')

[...]

Much better although with namedtuple, the attributes are fixed aren't
they? I don't use collections as much as I should.


-- 
Cordially,
Noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread kracekumar ramaraju
Yes. Attributes are fixed. The advantage over dictionary is ease of access
like p.foo rather than p['foo'] or p.get('foo').


On Fri, May 23, 2014 at 1:34 PM, Noufal Ibrahim KV
wrote:

> On Fri, May 23 2014, kracekumar ramaraju wrote:
>
> > You can use namedtuple.
> >
> > from collections import namedtuple
> > Person = namedtuple('Person', ['foo', 'bar', 'baz'])
> > p = Person(foo='foo', bar='bar', baz='baz')
>
> [...]
>
> Much better although with namedtuple, the attributes are fixed aren't
> they? I don't use collections as much as I should.
>
>
> --
> Cordially,
> Noufal
> http://nibrahim.net.in
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com *
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Rohit Chormale
R u sure @ 'ease of access' or is it 'ease of writing'?


On Fri, May 23, 2014 at 1:43 PM, kracekumar ramaraju <
kracethekingma...@gmail.com> wrote:

> Yes. Attributes are fixed. The advantage over dictionary is ease of access
> like p.foo rather than p['foo'] or p.get('foo').
>
>
> On Fri, May 23, 2014 at 1:34 PM, Noufal Ibrahim KV
> wrote:
>
> > On Fri, May 23 2014, kracekumar ramaraju wrote:
> >
> > > You can use namedtuple.
> > >
> > > from collections import namedtuple
> > > Person = namedtuple('Person', ['foo', 'bar', 'baz'])
> > > p = Person(foo='foo', bar='bar', baz='baz')
> >
> > [...]
> >
> > Much better although with namedtuple, the attributes are fixed aren't
> > they? I don't use collections as much as I should.
> >
> >
> > --
> > Cordially,
> > Noufal
> > http://nibrahim.net.in
> >
>
>
>
> --
>
> *Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
> Torvaldshttp://kracekumar.com *
> ___
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread kracekumar ramaraju
Rohit

 Probably ease of writing may be right here.


On Fri, May 23, 2014 at 1:46 PM, Rohit Chormale wrote:

> R u sure @ 'ease of access' or is it 'ease of writing'?
>
>
> On Fri, May 23, 2014 at 1:43 PM, kracekumar ramaraju <
> kracethekingma...@gmail.com> wrote:
>
> > Yes. Attributes are fixed. The advantage over dictionary is ease of
> access
> > like p.foo rather than p['foo'] or p.get('foo').
> >
> >
> > On Fri, May 23, 2014 at 1:34 PM, Noufal Ibrahim KV
> > wrote:
> >
> > > On Fri, May 23 2014, kracekumar ramaraju wrote:
> > >
> > > > You can use namedtuple.
> > > >
> > > > from collections import namedtuple
> > > > Person = namedtuple('Person', ['foo', 'bar', 'baz'])
> > > > p = Person(foo='foo', bar='bar', baz='baz')
> > >
> > > [...]
> > >
> > > Much better although with namedtuple, the attributes are fixed aren't
> > > they? I don't use collections as much as I should.
> > >
> > >
> > > --
> > > Cordially,
> > > Noufal
> > > http://nibrahim.net.in
> > >
> >
> >
> >
> > --
> >
> > *Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
> > Torvaldshttp://kracekumar.com *
> > ___
> > BangPypers mailing list
> > BangPypers@python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> >
> ___
> BangPypers mailing list
> BangPypers@python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>



-- 

*Thanks & Regardskracekumar"Talk is cheap, show me the code" -- Linus
Torvaldshttp://kracekumar.com *
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Noufal Ibrahim KV
On Fri, May 23 2014, kracekumar ramaraju wrote:

> Rohit
>
>  Probably ease of writing may be right here.

It's also more future proof. An attribute can be replaced by a property
which implements access controls and other things without breaking API
contracts. It's harder to do that while subscripting.

[...]


-- 
Cordially,
Noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Navin Kabra
"Mandar Vaze / मंदार वझे"  writes:

> Code 1:
> ...
> return dict(fname=fname, lname=lname, saluation=salutation,
>  gender=gender, addr1=addr1, addr2=addr2,
>  city=city, state=state, country=country)

First of all, both functions are returning a single value, a single
dict. So the entire discussion of "how many values should I return from a
function" is irrelevant.

As for which of the two code samples is better, I think it is a matter
of taste.

However, I should point out that in template programming (as happens in
the case of Django), it is fairly common to create and return a dict
containing all the values that are needed for the template. In other
words, it is a pattern that people are used to; and since they're all
values that are needed in the same template, it is OK to club them
together like this. To me, "Code #1" is perfectly acceptable - for this
use case. 

> Code 2:
> user = {}
[ 5 more citation lines. Click/Enter to show. ]
> user['fname'] = fname
> user['lname'] = lname
> ...
> ...
> ...
> user['country'] = country
>
> return dict(user=user)

I don't see why doing the dict assignments into different statements is
any better than simply creating the dict directly. In fact, I prefer the
former (but, I would put each "key: value" pair on a line by itself),
because the intent is much clearer.

Also, you could have done "return user" here instead of "return
dict(user=user)". Which one is better has nothing to do with python, and
depends upon the template language you're using and the content of the
template.

navin.
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Multiple return values from a function : Where to draw the line ?

2014-05-23 Thread Mandar Vaze / मंदार वझे
On Fri, May 23, 2014 at 5:26 PM, Navin Kabra  wrote:

> "Mandar Vaze / मंदार वझे"  writes:
>
> > Code 1:
> > ...
> > return dict(fname=fname, lname=lname, saluation=salutation,
> >  gender=gender, addr1=addr1, addr2=addr2,
> >  city=city, state=state, country=country)
>
> First of all, both functions are returning a single value, a single
> dict.


Yes, I realized that "after the fact" :(


> So the entire discussion of "how many values should I return from a
> function" is irrelevant.
>

The code samples were incorrect - but otherwise the discussion is relevant
I think. (Although I couldn't get direct answer)
Pointer to "dis" module is helpful.

-Mandar
___
BangPypers mailing list
BangPypers@python.org
https://mail.python.org/mailman/listinfo/bangpypers