Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unexpected expected type error result
#1
# creating a new kind of class or instance attribute

class Integer:
    def __init__(self, name):
        self.name = name

    def __get__(self, instance, cls):
        if instance is None:
            return self
        else:
            return instance.__dict__[self.name]

    def __set__(self, instance, value):
        if not isinstance(value, int):
            raise TypeError('expected an int')
        instance.__dict__[self.name] = value


    def __delete__(self, instance):
        del instance.__dict__[self]


class Point:
    x = Integer('x')
    y = Integer('y')
    def __init__(self, x, y):
        self.x = x
        self.y = y


p = Point(12, 3)
my_int = Integer(1)
print('myint:   {}   {}'.format(my_int, my_int.name))
print('{}   {}'.format(p.x, p.y))


my_int2 = Integer('1')
print('myint2:  {}   {}'.format(my_int2, my_int2.name))
# p2 = Point('1', 3)
my_int2 = Integer('1')
does not produce the expected type error
while uncommenting p2 does raise the expected error
TypeError: expected an int
Wall
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  I have Traceback error and a type error and i dont know how to fix it coltron1282 2 66 Feb-07-2019, 03:15 PM
Last Post: coltron1282
  Timer class not working as expected. MuntyScruntfundle 4 135 Feb-02-2019, 09:47 AM
Last Post: MuntyScruntfundle
  Unexpected ininite loop behavior RedSkeleton007 4 87 Jan-27-2019, 05:32 PM
Last Post: aakashjha001
  unexpected sub result after overloading operator jolinchewjb 1 91 Jan-24-2019, 08:23 AM
Last Post: buran
  unexpected EOF wowo 3 211 Jan-13-2019, 04:36 AM
Last Post: wowo
  Erratic Datetime result timsch 0 119 Dec-27-2018, 01:49 AM
Last Post: timsch
  float multiplication - unexpected output inesk 3 168 Dec-11-2018, 10:59 AM
Last Post: DeaD_EyE
  Unexpected result eftimios 1 183 Dec-02-2018, 07:39 AM
Last Post: Gribouillis
  pdb says "SyntaxError: unexpected EOF" on comment pwannh 1 155 Nov-29-2018, 04:17 PM
Last Post: nilamo
  want to change the beginning of the result Rudinirudini 5 214 Nov-15-2018, 11:28 AM
Last Post: Rudinirudini

Forum Jump:


Users browsing this thread: 1 Guest(s)