Quickly Learn How To SubClass A Python Type Created In Delphi With This Windows Sample App
import spam
class MyPoint(spam.Point):
def Foo(Self, v):
Self.OffsetBy(v, v)
# old way to create a type instance
p = spam.CreatePoint(2, 5)
print (p, type(p))
p.OffsetBy( 3, 3 )
print (p.x, p.y)
print (“Name =”, p.Name)
p.Name = ‘Hello world!’
print (“Name =”, p.Name)
# new way to create a type instance
p = spam.Point(2, 5) # no need to use CreateXXX anymore
print (p, type(p))
p.OffsetBy( 3, 3 )
print (p.x, p.y)
# create a subtype instance
p = MyPoint(2, 5)
print (p, type(p))
p.OffsetBy( 3, 3 )
print (p.x, p.y)
p.Foo( 4 )
print (p.x, p.y)
print (dir(spam))
print (spam.Point)
print (“p = “, p, ” –> “,)
if type(p) is spam.Point:
print (“p is a Point”)
else:
print (“p is not a point”)
p = 2
print (“p = “, p, ” –> “,)
if type(p) is spam.Point:
print (“p is a Point”)
else:
print (“p is not a point”)
p = spam.CreatePoint(2, 5)
try:
print (“raising an error of class EBadPoint”)
p.RaiseError() # it will raise spam.EBadPoint
except spam.PointError as what: # this shows you that you can intercept a parent class
print (“Caught an error derived from PointError”)
print (“Error class = “, what.__class__, ” a =”, what.a, ” b =”, what.b, ” c =”, what.c)
# You can raise errors from a Python script too!
print (“——————————————————————“)
print (“Errors in a Python script”)
try:
raise spam.EBadPoint(“this is a test!”)
except:
pass
try:
err = spam.EBadPoint()
err.a = 1
err.b = 2
err.c = 3
raise err
except spam.PointError as what: # this shows you that you can intercept a parent class
print (“Caught an error derived from PointError”)
print (“Error class = “, what.__class__, ” a =”, what.a, ” b =”, what.b, ” c =”, what.c)
if p == spam.CreatePoint(2, 5):
print (“Equal”)
else:
print (“Not equal”)