Python Forum
skeleton class needs working instantiation guard
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
skeleton class needs working instantiation guard
#2
Just raise an exception, like so

class Point:
 
    def __init__(self, x, y, z):
        self.x = x
        self.y = y
        self.z = z
 
    @property
    def x(self):
        return self.__x
 
    @x.setter
    def x(self, a):
        if isinstance(a, int):
            self.__x = a
        else:
            raise AttributeError("x must be an integer")
 
    @property
    def y(self):
        return self.__y
 
    @y.setter
    def y(self, b):
        if isinstance(b, int):
            self.__y = b
        else:
            raise AttributeError("y must be an integer")
 
    @property
    def z(self):
        return self.__z
 
    @z.setter
    def z(self, c):
        if isinstance(c, int):
            self.__z = c
        else:
            raise AttributeError("z must be an integer")

p = Point(1, 2, 3.5)
Reply


Messages In This Thread
RE: skeleton class needs working instantiation guard - by hshivaraj - Dec-09-2017, 09:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheritance vs Instantiation for Python classes mr_byte31 7 3,007 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Skeleton file export error Python Code pepapoha 4 3,570 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  What is the strategy for working with class variables? AlekseyPython 3 3,059 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Timer class not working as expected. MuntyScruntfundle 4 2,700 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  How are these methods working within this class workerbee 3 2,964 Nov-29-2017, 04:15 PM
Last Post: hshivaraj
  If statement [Guard List] yuvalsaias 7 6,257 Apr-17-2017, 04:19 PM
Last Post: nilamo
  Class attribute not recognized\working J125 1 5,442 Dec-19-2016, 01:05 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020