Tuesday, 15 March 2011

In python 3, how can I convert a function into an attribute -



In python 3, how can I convert a function into an attribute -

i have simple function can phone call this:

bln=getcoln(bl,n)

i'd phone call this:

bln=bl.getcoln(n)

the details don't matter, because i'd know how in general, since certainly ask: bl list of lists (a 2d array) , bln 1d list consisting of nth column of bl. yes, i'm sure can comprehension.

i'm using python 3.4 on win7 system.

what want step structured programming object oriented programming in syntax sense. older code may designed using oo principles (or oo approach can natural). firstly, bl must object if sec form used. object known instance of class. way, have define class.

then, bl variable represents object. in python, reference object. object created when call class name. bl name of object outside class definition. name of same object within class definition named self convention.

to set together, function converted fellow member function of class, , should rename arr (the first argument in old function) in old function definition self (to stick conventions). however, if bl (from outside of function definition, or arr within of function definition) list in old code, becomes object reference. think object lean wrapper around list. there 2 ways (composition , inheritance). usually, composition should prefered way when such conversion (unless know doing). means object must store before reference list. in class definition, utilize self.arr (or more suitable name) fellow member variable storing reference list. that, have implement special method (a method named fellow member function) named __init__ (the underscores doubled):

class myarray: def __init__(self, arr): self.arr = arr

in python, self. prefix must used explicitly within class definition if want refere fellow member variables or fellow member functions.

now add together getcoln:

def getcoln(self, n): ... alter body slightly...

say had def getcoln(arr, n) in existing function. arr reference list of lists. now, first argument of function must reference object (i.e. class instance), , array named self.arr. way, should rename before arr in function body self.arr. have:

class myarray: def __init__(self, arr): self.arr = arr def getcoln(self, n): ... changed body...

you can utilize new class definition wrap existing list of lists in sense pass old construction constructed object:

x = myarray(bl)

the x new replacement old bl. old bl passed __init__ -- seen arr within class definition, , saved self.arr.

and can call:

bln = x.getcoln(n)

i did utilize x identifier emphasize difference only. when refactoring (rewriting old code), may want prefer maintain bl identifier -- reuse different purpose:

bl = myarray(bl)

the bl argument list of lists, later becomes object reference.

when works, can add together implementation of other special methods, , can utilize syntax object of own class if array (here using x 1 time again emphasize idea):

bln = x[n]

python-3.x attributes user-defined-functions

No comments:

Post a Comment