Elad Elmakias
Elad Elmakias

Reputation: 37

python is there a way to set a default value to an arguent to be another arguement in the function?

im trying to build a matix class for practice, and i was wondering, suppose I define a matrix with the arguements "rows" and "columns". is there a way to set "columns" to be "rows" as default? example:def __init__(self, rows, columns = rows) i tried to compile but it gives me the "unresolved refence" error thank you

Upvotes: 3

Views: 40

Answers (1)

Erich
Erich

Reputation: 1848

This does not work. But you can set None as default and check for that in the method's body, e.g.

def __init__(self, rows, columns=None):
    if columns is None:
        columns = rows
    # do something

Upvotes: 5

Related Questions