python - Assign attribute to function -


this beginner question, have question attributes. have module responsible google docs api actions contains functions retrieving information. able refer variables these functions attributes. here example:

gdocs.py

def getrows():     rows = #action getting rows     rowstext = #action converting text 

general.py

import gdocs text = gdocs.getrows.rowstext 

i know basic effect of passing variables can achieved returning values, refer them attributes if possible. put, question is, how can create attribute of function can reference in .py document?

thanks , sorry if has been answered, did try search kept running nto specific problems.

it sounds if want return result consisting of multiple parts. don't use function attributes this, return new object can addressed via attributes instead. that'd make thread-safe well, function attributes live in same scope function object itself: global.

the standard library has helpful class factory function such return values, collections.namedtuple():

from collections import namedtuple  point = namedtuple('point', 'x y')  def calculate_coordinates(foo, bar, baz):     return point(42, 81) 

the return value tuple subclass, can addressed tuple (with indexing), can unpacked separate values, or can use attributes:

result = calculate_coordinates(spam, ham, eggs) print result.x, result.y 

or

res_x, res_y = calculate_coordinates(spam, ham, eggs) 

all work.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -