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
#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 :)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Unexpected (?) result with regular expressions guraknugen 2 63 Jan-18-2020, 02:33 PM
Last Post: guraknugen
  Type hinting - return type based on parameter micseydel 2 86 Jan-14-2020, 01:20 AM
Last Post: micseydel
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 128 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Getting an "Unexpected Token" Error and don't know why... NotAHackusator 1 106 Nov-20-2019, 03:00 PM
Last Post: buran
  applying and(&) ,or(|) in conditions does not result output correctly as expected Smiling29 4 223 Oct-21-2019, 01:39 AM
Last Post: ichabod801
  Need help with "unexpected expression syntax error" PyCoder 2 367 Sep-29-2019, 05:42 AM
Last Post: PyCoder
  Post JSON from python to PHP don't give expected result pgagnebin 1 171 Sep-04-2019, 10:29 PM
Last Post: micseydel
  Type Error: bad operand type for unary +: 'str' Psypher1 2 1,780 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  catch input type error mcmxl22 5 320 Aug-11-2019, 07:33 AM
Last Post: wavic
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 2,036 Jul-13-2019, 10:21 AM
Last Post: jaycuff13

Forum Jump:


Users browsing this thread: 1 Guest(s)