On Fri, 03 Feb 2006 08:16, you wrote:
> >python
>
> Python 2.4 (#1, Dec 4 2004, 20:10:33)
> [GCC 3.3.3 (cygwin special)] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> class Foo(object):
>
> ... pass
> ...
>
> >>> f = Foo()
> >>> f is Foo # false. f as an instance of Foo, not Foo itself
>
> False
>
> >>> type(f) == Foo
>
> True
>
> >>> type(f) is Foo
>
> True
Thanks for the replies (Carl & Jim).
For the example:
class Foo(Object):
works, but for my implementation:
class Foo:
doesn't.
type(f) returns 'type <instance>', which is no good.
And I can't do this:
class Foo():
Because empty parentheses are not allowed.
I was thinking of using 'isinstance', which does what I want, but Googling
around tells me it might not be a Good Thing.
> WARNING: Doing this may result in brittle programs. What, say, later
> on you want to make
> a wrapper around Foo? then type(f) == FooWrapper, not Foo.
>
> If there are particular attributes an object must have, check thay are
> there with hasattr. Then the type of f can be changed to whatever
> later on, and your program has more of a chance of working without
> other changes.
I really want to be sure that the object I am looking at is of a specific
class. I am writing a class that can operate on strings or another class.
In my code I have a test:
if type(foo) == str:
# Handle a string
elif type(foo) == <My class>:
# Handle My class
else:
raise "This function can only handle strings and <My Class>"
Obviously foo is an argument passed to a function, and I want the function
to be able to accept strings and my own class and do the Right Thing.
Andy