Mailing List Archive

Asserting object class
In a method, I'd like to assert that a parameter is an object which is
of a specific class or has this class as it's base class.

I can test on the __class__ and its __bases__ attributes, but its clumsy
and I'm wondering if there is an easier notation, like:

def method(self, p)
assert class(p) == class(self)

This is comparable to - in a strongly typed language - specifying a
required base class as the type of the parameter.

Sincerely,
Anders Dinsen

--
i-data is a leading vendor of printing connectivity solutions,
e-forms software, and networking products.
More information is available at www.i-data.com

Disclaimer: I speak for myself, not my employer.
Asserting object class [ In reply to ]
Adi <adi@i-data.com> writes:

> In a method, I'd like to assert that a parameter is an object which is
> of a specific class or has this class as it's base class.
>
> I can test on the __class__ and its __bases__ attributes, but its clumsy
> and I'm wondering if there is an easier notation, like:
>
> def method(self, p)
> assert class(p) == class(self)
>
> This is comparable to - in a strongly typed language - specifying a
> required base class as the type of the parameter.

I think that this will do what you want:

def method(self,p):
assert isinstance(p,self.__class__)

the following also does the same:

def method(self,p):
assert issubclass(p.__class__,self.__class__)

HTH
Michael
Asserting object class [ In reply to ]
Thank you, but:

Michael Hudson wrote:

> Adi <adi@i-data.com> writes:
>
> > In a method, I'd like to assert that a parameter is an object which is
> > of a specific class or has this class as it's base class.
>
> I think that this will do what you want:
>
> def method(self,p):
> assert isinstance(p,self.__class__)
>

Actually it does'nt. When this method is called, it is called on a subclass
of the superclass I wan't to assert the parameter is of.

So it fails. What I do now is:

class X:
def m(self, p):
assert isinstance(p, X)
...

>
> the following also does the same:
>
> def method(self,p):
> assert issubclass(p.__class__,self.__class__)
>

I have'nt tested, but I suspect it will do the same.

Anders

--
i-data is a leading vendor of printing connectivity solutions,
e-forms software, and networking products.
More information is available at www.i-data.com

Disclaimer: I speak for myself, not my employer.
Asserting object class [ In reply to ]
On Thu, 10 Jun 1999 09:46:46 +0000, Adi <adi@i-data.com> wrote:

> In a method, I'd like to assert that a parameter is an object which is
> of a specific class or has this class as it's base class.
>
> I can test on the __class__ and its __bases__ attributes, but its clumsy
> and I'm wondering if there is an easier notation, like:
>
> def method(self, p)
> assert class(p) == class(self)
>
> This is comparable to - in a strongly typed language - specifying a
> required base class as the type of the parameter.

FWIW, in Dylan you'd write it as:

define method m (self, p)
assert(object-class(p) == object-class(self));
..
end;

__Jason