Sunday, 15 January 2012

list - how to add methods and arguments to existing Python class -



list - how to add methods and arguments to existing Python class -

i have function returns special list has been prepared according function's options:

def matrix( numberofcolumns = 3, numberofrows = 3, element = 0.0 ): matrix = [] column in range(numberofcolumns): matrix.append([element] * numberofrows) return(matrix)

i want create new class based on existing class list , add together various methods , various arguments in order create special list, much in way shown in function above. can create new class , add together methods in way such following:

class matrix(list): def __init__(self, *args): list.__init__(self, *args) def printout(self): return("printout")

i not sure how add together new arguments new class. class list accepts 1 positional argument , i'm not sure how add together more class in sensible way.

specifically, want able create instance of new class in way such following:

a = matrix(numberofcolumns = 3, numberofrows = 2)

i have class set in next way:

class matrix(list): def __init__( self, numberofcolumns = 3, numberofrows = 3, element = 0.0, *args ): self.numberofcolumns = numberofcolumns self.numberofrows = numberofrows super().__init__(self, *args) def printout(self): return("printout")

when instantiate in next way:

a = matrix(numberofcolumns = 3)

i encounter next error:

typeerror: super() takes @ to the lowest degree 1 argument (0 given)

when instantiate in next way:

a = matrix("1", numberofcolumns = 3)

i encounter next error:

typeerror: __init__() got multiple values keyword argument 'numberofcolumns'

where going wrong?

first, should not name function and class matrix.

you can add together argument class in every other class, add together __init__ method , give others list.__init__:

class matrix(list): def __init__(self, numberofcolumns, numberofrows, *args): super().__init__(self, *args) self.numberofcolumns = numberofcolumns self.numberofrows = numberofrows def printout(self): return("printout")

here matrix constructor takes 2 positional arguments (numberofcolumns , numberofrows) , unknown number of additional positional arguments. give additional arguments (and only) list's constructor, , add together numberofcolumns , numberofrows attributes class.

after edit, seem want utilize keyword arguments in constructor, should defined after every positional arguments, this:

def __init__(self, *args, numberofcolumns = 3, numberofrows = 3, element = 0.0): super().__init__(self, *args) self.numberofcolumns = numberofcolumns self.numberofrows = numberofrows self.element = element # guess want `element` attribute of class understanding error

to understand error got edit (typeerror: __init__() got multiple values keyword argument 'numberofcolumns') let's utilize short illustration function:

>>> def foo(positional, kw='default', *args): ... print(positional, kw, *args) ... >>> foo(1) 1 default >>> foo(1, 2) 1 2 >>> foo(1, 2, 3) 1 2 3 >>> foo(1, kw='bar') 1 bar >>> foo(1, 2, kw='bar') traceback (most recent phone call last): file "<stdin>", line 1, in <module> typeerror: foo() got multiple values keyword argument 'kw'

the arguments give when calling foo assigned left right, if argument keyword argument. thus, when calling foo(1, 2), assign 1 positional , 2 kw, , when calling foo(1, 2, kw='bar') giving kw 2 values: 1 position (2) , 1 keyword ('bar').

foo improve defined this:

>>> def foo(positional, *args, kw='default'): ... print(positional, kw, *args) ... >>> foo(1) 1 default >>> foo(1, 2) 1 default 2 >>> foo(1, 2, 3) 1 default 2 3 >>> foo(1, kw='bar') 1 bar >>> foo(1, 2, kw='bar') 1 bar 2

that way don't have worry assigning keyword argument position mistake.

for reference: some explanation on *args , **kwargs

python list class methods

No comments:

Post a Comment