Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Python default arguments/attributes

Name: Anonymous 2013-03-07 15:23

Hey /prog/,
What's best practice when overriding defaults with arguments?

How do you do it?

Either way, it seems like a lot of boilerplate code.



class foo(object):
    bar = 5
    baz = 3


    def __init__(bar=None, baz=None, boo=1):

        # what if there are more possible values?
        self.bar = self.bar if bar is None else bar
       
        # what if baz isn't None, but evaluates to False?
        self.baz = baz or self.baz

        self.boo = boo

Name: Anonymous 2013-03-07 15:25

>>1
Best practice is to read SICP

Name: Anonymous 2013-03-08 15:03

what's wrong with def __init__(bar=5, baz=3, boo=1): ?

Name: Anonymous 2013-03-08 15:08

(define (foo #!key (bar 5) (baz 3) (boo 1)) #&body)

Name: Anonymous 2013-03-08 15:14

To elaborate on >>3:

class foo(object):
    def __init__(bar=5, baz=3, boo=1):
        self.bar, self.baz, self.boo = bar, bas, boo

Name: Anonymous 2013-03-08 15:51

>>1
Just write "def __init__(bar=5, baz=3, boo=1):", moron.

Name: Anonymous 2013-03-09 3:32

>>6
bar, baz can be of boxed type, moron.  >>1 asks a valid question, but knows too little Python to ask it correctly.

Name: Anonymous 2013-03-09 9:56

>>7
Bullshit. There's no case in which >>6 would not work.

Name: Anonymous 2013-03-09 10:54

>>7
boxed type
Python
And condescending "but knows too little Python to ask it correctly" makes your comment a work of art.
>>8
Well, strictly speaking, what if OP is in a habit of changing default values at runtime while he gorges on an enormous penis?

Name: Anonymous 2013-03-09 13:03

lol default arguments with mutable objects

js ftw

Name: Anonymous 2013-03-09 13:39

>>9
Then it's just bad code.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List