I'm trying to understand (and re-use) some Python code.
In general I find Python very easy to understand (one of the reasons I like it) but I'm a bit unclear about one bit of code which is a callback function, it is as follows:-
# # # # def cb_prepare(args): request = args["request"] data = request.getData() data["statcatslinks"] = PyblStatcats(request)
It doesn't matter that it's a callback function (I can follow how/why it gets called) and I can understand the first two lines. The first line sets 'request' and the second gets the data dictionary from the request object.
What I'm not clear about is the last line, PyblStatcats is a class. As I understand it 'PyblStatcats(request)' creates an instance of PyblStatcats and runs its __init__ method, so far so good. But what does 'PyblStatcats(request)' return? It would appear to be a string as it's used to set the value in the data dictionary. However I can't find anywhere in the Python documentation that tells me about this (implicit?) return value.
On Monday 11 June 2007 10:26:35 Chris G wrote:
# def cb_prepare(args): request = args["request"] data = request.getData() data["statcatslinks"] = PyblStatcats(request)
What I'm not clear about is the last line, PyblStatcats is a class. As I understand it 'PyblStatcats(request)' creates an instance of PyblStatcats and runs its __init__ method, so far so good. But what does 'PyblStatcats(request)' return? It would appear to be a string as it's used to set the value in the data dictionary. However I can't find anywhere in the Python documentation that tells me about this (implicit?) return value.
If PyblStatcats is a class, then calling it must return an instance of PyblStatcats. If you include a return statement in a class's __init__ method, it can only return None, anything else raises an exception.
Are you sure that this call is returning a string? Might it be returning an object which has an explicit __str__ method? What does
py> data["statcatslinks"].__class__
say?
Cheers, Richard