0
def CreateText(win, text, x, y, size, font, color, style):
    txtObject = Text(Point(x,y), text)
    if size==None:
        txtObject.setSize(12)
    else:
        txtObject.setSize(size)

    if font==None:
        txtObject.setFace("courier")
    else:
        txtObject.setFace(font)

    if color==None:
        txtObject.setTextColor("black")
    else:
        txtObject.setTextColor(color)

    if style==None:
        txtObject.setStyle("normal")
    else:
        txtObject.setStyle(style)

    return txtObject

def FlashingIntro(win, numTimes):
    txtIntro = CreateText(win, "CELSIUS CONVERTER!", 5,5,28)
    for i in range(numTimes):
        txtIntro.draw(win)
        sleep(.5)
        txtIntro.undraw()
        sleep(.5)

I'm trying to get the CreateText function to create a text object with my "default" values if the parameters are not used. I want the fallback text to be 12pt black courier. I've tried it with blank strings "" instead of None and no luck. I'm fairly new to Python and have little programming knowledge.

agf
  • 171,228
  • 44
  • 289
  • 238
Eli Nahon
  • 29
  • 1
  • 6
  • Where did you get the idea that a "default argument" even exists in the first place? It's hard for me to imagine a reference for Python that somehow mentions them without describing how to set them up. – Karl Knechtel Apr 12 '12 at 05:27
  • If I don't type arguments for size, style, etc. in CreateText(win, text, x, y, size, style, ...) function, Python automatically sets size and style to 12 pt(?) and normal font style. – Eli Nahon Apr 12 '12 at 06:53
  • That doesn't answer what I asked at all. Where did you first hear the term "default argument"? Where did you get the idea that this is a thing that Python can do? How did that place manage not to tell you how to do it? – Karl Knechtel Apr 12 '12 at 10:24

4 Answers4

2

In python we put default values for arguments/keywords directly in the function signature, ie:

def CreateText(win, text, x, y, size=12, font="courier", color="black", style="normal"):
    #do stuff

You can set a default value to None, ie size=None, and there are some cases where that's useful, but in this case you don't need to. Arguments without default values are required, and if they are not present at call time an error is raised (as you've just seen).

Update:

As @agf has pointed out, you should not set mutable defaults in the signature if your function modifies or returns them. In this case it's a good idea to use argument=None.

Bi Rico
  • 25,283
  • 3
  • 52
  • 75
  • 1
    So long as the [defaults are immutable](http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument) this is fine, but the `if argument is None` version is frequently used to avoid surprises. – agf Apr 12 '12 at 05:10
1

You need to specify default values for the arguments:

def CreateText(win, text, x, y, size=None, font=None, color=None, style=None):

or, putting your real defaults in so you don't need if statments:

def CreateText(win, text, x, y, 
                         size=12, font="courier", color="black", style="normal"):

    txtObject = Text(Point(x,y), text)
    txtObject.setSize(size)
    txtObject.setFace(font)
    txtObject.setTextColor(color)
    txtObject.setStyle(style)

    return txtObject
agf
  • 171,228
  • 44
  • 289
  • 238
0

Set the defaults in the function definition:

def CreateText(win, text, x, y, size=None, font=None, color=None, style=None):

Also, in Python it is best to test singleton objects like None with:

if size is None:

etc...

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

I'm trying to get the CreateText function to create a text object with my "default" values if the parameters are not used.

The error tells you exactly what the problem is. You can't change the correct way to call the function (currently: with 8 arguments) by changing the contents of the function. You must change the interface to the function (i.e. the def line). Arguments to a function only have default values if you give them default values.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • I'm pretty sure that's exactly what he's asking how to do, he's just not asking it very clearly. – agf Apr 12 '12 at 05:32