[Tutor] constructing semi-arbitrary functions

Oscar Benjamin oscar.j.benjamin at gmail.com
Mon Feb 17 20:58:00 CET 2014


On Feb 17, 2014 7:24 PM, ""André Walker-Loud
<walksloud at gmail.com>""
<walksloud at gmail.com> wrote:
>
> Question 1:
> Is there a better way to accomplish (my hopefully clear) goals?

I'm not sure that there is given the constraints you're under from the
third party function.

> Question 2:
> In method 1, is there a way to combine the two classes, or is the
FunctionStruct necessary?  I want to do something *like*
>
> self.func_code.co_varnames = tuple(['c_%i'%(i) for i in range(len(pars))])
> self.func_code.co_argcount = len(pars)
>
> but I was not sure how to first define 'self.func_code'.  Can you define
it as an empty container somehow (is that the right way to describe it)?

I'm not sure about the details of this. Maybe Eryksun will chime in there...

> Question 3:
> The 2nd method is in some sense simpler - I just hack together a string,
but I need to use global variables, which I know can be problematic.  Which
method, 1 or 2, is in general better?  One answer, is whatever works, but I
am trying to also improve my programming skills, so I am wondering from a
software perspective, which is preferable?

Either solution if it works for this case seems fine to me. I'd be inclined
to in for the simpler compile/exec approach.

The "right" solution is to change the interface of the third party
function. It is poorly designed and should not be inspecting those function
attributes or it should at least provide an option for you to provide that
information in a different way. Assuming it's open source you could try
sending a patch. In the mean time you could copy that code and modify it to
suit your purposes.

Oscar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140217/981c9d86/attachment.html>


More information about the Tutor mailing list