__setitem__ implementation in Python for Point(x,y) class

前端 未结 7 986
故里飘歌
故里飘歌 2020-12-14 12:44

I\'m trying to make a Point class in python. I already have some of the functions, like __ str__ , or __ getitem__ implemented, and it works great. The only problem I\'m fac

7条回答
  •  感情败类
    2020-12-14 13:26

    Your may find it a lot easier to use namedtuple for this:

     from collections import namedtuple
     Point= namedtuple('Point', ['x','y'])
    
     fred = Point (1.0, -1.0)
     #Result: Point(x=1.0, y=-1.0)
    

    The main drawback is that you can't poke values into a namedtuple - it's immutable. In most applications that's a feature, not a bug

提交回复
热议问题