On Fri, Mar 20, 2009 at 10:32 PM, Yau Kwan Kiu <[email protected]> wrote:
>
> Hello.
> I am new to SymPy and I don't know if you have the Graph class in
> SymPy, but I am playing with my Graph.py anyway (see below).

We don't, as far as I know and it would be cool to have it. Thanks for doing it.

>
> I have a question in the use of self in  defining __init__: in a class.
>
> I want to define a graph, and first I define vertex objects and edge objects.
> The edges should be attributed their endpoints (which are vertex objects)
> and the vertices should be attributed their incident edges (which are
> edge objects)
>
> In the following, I try to define the vertex object first, then define
> the edges (containing
> incidence information).  In the process of defining the edges, I try
> to update the vertex objects
> with the new incidence information with following snippet:
>
> <code>
> class Edge:
>    def 
> __init__(self,endpoint1="",endpoint2="",label='f',endpoints=set([]),beingObject='itself'):
> ...
> ...
>            for i in self.endpoints:         #usually we define the
> vertices before the edges
> ...
>                """jazz up the incident endpoints"""
>                i.edges.add(self)
> </code>

Yes, the problem is that i.edges.add(self) sets this to all your edges.

The way out is to use the same design as we have in sympy --- all
instances are immutable. Thus you should never do anything like
i.edges.add(), but rather create a new modified instance.

Ondrej

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/sympy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to