On 9/8/10 9:40 PM, Innigo wrote:
Thanks, this is exactly what I want. I do want to be modular. However,
my graphics object
contains 0 graphics primitives

g = Graphics()
def f1(g)
     g += polygon(...)
def f2(g)
     g += ...
show(g)

Can I not pass g as a parameter and have it returned to outer scope?


One thing to realize about the above code is that you just define the functions f1 and f2, but never call them. So the functions never actually do anything. Another thing is that when you define the function as "def f1(g)", the g inside the function definition refers to the g passed into the function, not the global variable g.

Usually, it is better style for function to not modify their inputs. I'd suggest doing something like:

def my_line():
    p=Graphics()
    p+=line([(0,0),(1,1)])
    p+=line([(1,0),(0,1)]) #another line
    return p

def my_plot():
    return plot(x^2,(x,0,1))

picture=Graphics()
picture+=draw_line()
picture+=my_plot()
show(picture)

(each function creates a graphics object, and I just call the functions and add the results to create a new figure).

Jason

--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to