Python Forum
Unexpected expected type error result
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
Reply
#2
I wasn't familiar before with these magic methods but here's what I learned...

You've constructed Integer such that in Point, when self.x and self.y are assigned (or "__set__"), that the TypeError-raising code gets called. Merely constructing an integer directly only invokes its initializer, not its __set__ method. So this code appears to be working as it ought to. (I'm not as sure about __get__ or __delete__, but that's not what your question seems to be about.)
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Incorrect Type Error milkycow 4 282 Jun-25-2021, 06:04 AM
Last Post: milkycow
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 699 Jun-15-2021, 08:16 AM
Last Post: Anldra12
Star Type Error: 'in' object is not callable nman52 3 527 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 610 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 472 Mar-30-2021, 06:34 PM
Last Post: buran
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 797 Feb-09-2021, 05:40 PM
Last Post: soft
  error "IndentationError: expected an indented block" axa 4 916 Sep-08-2020, 02:09 PM
Last Post: ibreeden
  Unexpected syntax error djwilson0495 7 916 Aug-25-2020, 01:34 PM
Last Post: deanhystad
  Type Error or Value Error? spalisetty06 3 743 Jul-21-2020, 04:56 AM
Last Post: deanhystad
  Unexpected syntax error djwilson0495 5 911 Jul-20-2020, 04:23 PM
Last Post: buran

Forum Jump:

User Panel Messages

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