0

I am working on a 3d rendering engine using only the standard libraries and tkinter, but I've hit a snag when trying to define a function that merges the class that it is a part of with another instance of the class.

The code

class VertexCloud:

    def __init__(self, seed: Vertex = Vertex(Point(0, 0, 0), Point(0, 0, 0))):
        vertecies.append(seed)

#...

    #merge two vertex clouds
    def merge(vc: VertexCloud = VertexCloud(Vertex(Point(0, 0, 0), Point(0, 0, 0)))):
        for vertex in vc.verticies:
            verticies.append(vertex)

returns this error:

Traceback (most recent call last):
...
File"<Path>/ThreeDFamilies.py", line 126, in VertexCloud
    def merge(vc: VertexCloud = VertexCloud(Vertex(Point(0, 0, 0), Point(0, 0, 0)))):
NameError: name 'VertexCloud' is not defined

My only ideas of how to fix this are to either declare the class ahead of time (preferred), or define the function outside of the class. Unfortunately - although I am proficient in other languages - this is my first major project in python, so I don't have a full understanding of how this would work syntactically.

Any help would be much appreciated!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
roger
  • 55
  • 1
  • 8
  • 1
    This case is documented in the [PEP 484 - Type Hints](https://www.python.org/dev/peps/pep-0484/#the-problem-of-forward-declarations) – Laurent LAPORTE Mar 28 '20 at 06:44
  • Does this answer your question? [How do I specify that the return type of a method is the same as the class itself?](https://stackoverflow.com/questions/33533148/how-do-i-specify-that-the-return-type-of-a-method-is-the-same-as-the-class-itsel) – Laurent LAPORTE Mar 28 '20 at 06:50
  • That's not going to be enough. The default values have the same problem. – user2357112 Mar 28 '20 at 06:52
  • 1
    It doesn't seem to make any sense for `merge` to *have* a default. Also, `self` is missing in at least 3 places, and `vertices` is misspelled. – user2357112 Mar 28 '20 at 06:54
  • Thank you all for your submissions! I've got a lot of changes ahead of me, since this was only one of many functions with the same error that I've been putting off fixing. I'll post a solution once I have it working. – roger Mar 29 '20 at 04:46

0 Answers0