Re: Difference between type and class



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
.



Relevant Pages

  • Re: Difference between type and class
    ... If your confusion is of a more general nature I suggest reading the introduction of `Design Patterns', under `Specifying Object Interfaces'. ... def 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'. ...
    (comp.lang.python)
  • Re: Object interfaces and capabilities [was Re: File names and file objects [was Re: My Python annoy
    ... situation which would benefit from interfaces: ... def test_for_file: ... relatively easy verification of object "capabilities", ...
    (comp.lang.python)
  • Re: C# Inheritance and Interfaces
    ... > interface ITextBox: IControl ... > Can the designers of the language ponder over this and ... > maybe think of a way to remove this confusion in future ... Interfaces in C# are very similar to interfaces in Java. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: DHCP for multiple subnets (was: Re: Routing and bandwidth problem)
    ... >confusion could be caused by an interface the daemon never sees. ... Now, when investigating dhcpd for multiple interfaces and subnets, I was ... to avoid some possible confusion on dhcpd's part... ...
    (Fedora)
  • Re: DHCP for multiple subnets (was: Re: Routing and bandwidth problem)
    ... >>Well, I won't argue with the maintainers, though I can't figure out what ... >>confusion could be caused by an interface the daemon never sees. ... > assigning addresses and limited the interfaces to which the server process ... > Now, when investigating dhcpd for multiple interfaces and subnets, I was ...
    (Fedora)