class - Python: typecasting a variable in an object method declaration? -
i'm new python, , have class such following:
class myclass: var1 = 0 var2 = 1 var3 = 2 def function(self, x): x.method(...yadda...) for class, x object containing data , methods, , i'd able call methods within instance of myclass. there way specify x here object of type? instance, regularly 1 use following define object (from imported file called classfile):
newobj = classfile.myclass() for object x in code above, can require of specific object type, built-in object or 1 define? how specify this?
i want code throw error or bypass object sent in not of desired type, preferably before start attempting call methods in (i.e. x.method()).
a better practice "implement" duck typing. in case
def function(self, x): try: x.method(... yada ...) except attributeerror: raise myapierror
Comments
Post a Comment