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

Rohit Chormale rohitchormale at gmail.com
Fri May 23 09:34:48 CEST 2014


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 <shankhabanerjee at gmail.com> 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 / मंदार वझे
> <mandarvaze at gmail.com> 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 at python.org
> > https://mail.python.org/mailman/listinfo/bangpypers
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> https://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list