Re: Difference between type and class
- From: Nikolaus Rath <Nikolaus@xxxxxxxx>
- Date: Thu, 31 Jul 2008 14:27:25 +0200
Thomas Troeger <thomas.troeger.ext@xxxxxxxxxxx> writes:
Can someone explain to me the difference between a type and a class?
If your confusion is of a more general nature I suggest reading the
introduction of `Design Patterns' (ISBN-10: 0201633612), under
Specifying Object Interfaces'.
In short: A type denotes a certain interface, i.e. a set of
signatures, whereas a class tells us how an object is implemented
(like a blueprint). A class can have many types if it implements all
their interfaces, and different classes can have the same type if they
share a common interface. The following example should clarify
matters:
class A:
def bar(self):
print "A"
class B:
def bar(self):
print "B"
class C:
def bla(self):
print "C"
def foo(x):
x.bar()
you can call foo with instances of both A and B, because both classes
share a common type, namely the type that has a `bar' method), but not
with an instance of C because it has no method `bar'. Btw, this
example shows the use of duck typing
(http://en.wikipedia.org/wiki/Duck_typing).
That would imply that I cannot create instances of a type, only of
a class that implements the type, wouldn't it?
But Python denotes 'int' as a type *and* I can instantiate it.
Still confused,
-Nikolaus
--
»It is not worth an intelligent man's time to be in the majority.
By definition, there are already enough people to do that.«
-J.H. Hardy
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C
.
- Follow-Ups:
- Re: Difference between type and class
- From: Thomas Troeger
- Re: Difference between type and class
- References:
- Difference between type and class
- From: Nikolaus Rath
- Re: Difference between type and class
- From: Thomas Troeger
- Difference between type and class
- Prev by Date: Re: working pylint anyone?
- Next by Date: Re: SVN access with pysvn under Cygwin (Installation problems)
- Previous by thread: Re: Difference between type and class
- Next by thread: Re: Difference between type and class
- Index(es):
Relevant Pages
|