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
#1


I've created a skeleton Point class for points in a 3D space (e.g. with x, y, and z co-ordinates). I want the co-ordinates to be integers and for any other input value to be refused. That part works fine. I just can't figure out a way to have the class constructor refuse to initialize an object if any of the three inital values is not an integer. How do I do that? Here's my code so far:

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:
            print("x must be an integer")
            return None

    @property
    def y(self):
        return self.__y

    @y.setter
    def y(self, b):
        if isinstance(b, int):
            self.__y = b
        else:
            print("y must be an integer")
            return None

    @property
    def z(self):
        return self.__z

    @z.setter
    def z(self, c):
        if isinstance(c, int):
            self.__z = c
        else:
            print("z must be an integer")
            return None
Reply
#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
#3
I tried this:

b = Point(0.5, 3, -7)
With this result:

TypeError: __init__() missing 1 required positional argument: 'z'
While that attained my goal of not having the Point object instantiated with one or more invalid parameters, the error message is confusing. Is there something I can do about that?
Reply
#4
That peculiar. Could you post your class Point again?
I dont that error message, and see the expected behaviour.

b = Point(0.5, 3, -7)
Output:
Traceback (most recent call last): File "main.py", line 41, in <module> b = Point(0.5, 3, -7) File "main.py", line 4, in __init__ self.x = x File "main.py", line 17, in x raise AttributeError("x must be an integer") AttributeError: x must be an integer exited with non-zero status
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Inheritance vs Instantiation for Python classes mr_byte31 7 2,789 Oct-14-2021, 12:58 PM
Last Post: mr_byte31
  Skeleton file export error Python Code pepapoha 4 3,423 Nov-17-2020, 02:06 AM
Last Post: pepapoha
  What is the strategy for working with class variables? AlekseyPython 3 2,947 Feb-24-2019, 05:34 AM
Last Post: AlekseyPython
  Timer class not working as expected. MuntyScruntfundle 4 2,590 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  How are these methods working within this class workerbee 3 2,885 Nov-29-2017, 04:15 PM
Last Post: hshivaraj
  If statement [Guard List] yuvalsaias 7 6,131 Apr-17-2017, 04:19 PM
Last Post: nilamo
  Class attribute not recognized\working J125 1 5,292 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