specify types in method calls

U

Uwe Mayer

Hi,

sometimes I'd like to be able to specify that a parameter to a method call
should be of a specific instance. I can do that using the isinstance()
buildin in the body of the method.

Is there also a way of specifying this in the method definition, i.e.

class A(object):
def foo( name : B ):
...

which then would be equivalent to i.e.

class A(object):
def foo( name ):
assert isinstance( name, B )
....

Is anything like that already available? Or might that be possible in future
versions of Python?

Ciao
Uwe
 
G

Gerrit Holl

Uwe said:
Is there also a way of specifying this in the method definition, i.e.

class A(object):
def foo( name : B ):
...

which then would be equivalent to i.e.

class A(object):
def foo( name ):
assert isinstance( name, B )
....

Is anything like that already available?

No, it isn't.
Or might that be possible in future versions of Python?

Who knows :). But personally, I don't think it's very likely, because
it's syntax sugar for something that is, I believe, not very common.
(I think it's not very Pythonic, but I can't explain why)

Gerrit.
 
M

Mark McEahern

Uwe said:
Hi,

sometimes I'd like to be able to specify that a parameter to a method call
should be of a specific instance. I can do that using the isinstance()
buildin in the body of the method.

The Pythonic way seems to prefer a polymorphic approach; that is, rather
than focus on what type things are, the real question is whether they
behave in an expected way. Here's a reference to PyProtocols and PEP 246:

http://peak.telecommunity.com/PyProtocols.html

// m
 
J

John Roth

Uwe Mayer said:
Hi,

sometimes I'd like to be able to specify that a parameter to a method call
should be of a specific instance. I can do that using the isinstance()
buildin in the body of the method.

Is there also a way of specifying this in the method definition, i.e.

class A(object):
def foo( name : B ):
...

which then would be equivalent to i.e.

class A(object):
def foo( name ):
assert isinstance( name, B )
....

Is anything like that already available? Or might that be possible in future
versions of Python?

It's certainly not standard Python. I believe there are several
different projects that have implemented some form of type
checking.

The other question is more interesting. At one point there was a
types sig that kind of meandered along without producing any
result.

The advent of TDD (test driven development) has shown that
static typing does not show as much benefit as its proponents
claim. There are even some people (definitely in the minority,
I think) that say that the cost is more than the remaining benefit.

I think that the Python developers would be quite receptive
to proposals in that direction as long as they didn't change
the essential nature of Python. That seems to be remarkably
hard to do. There is some work being done in that direction
in the PyPy project, and the type inference mechanism in
Bycicle Repair Man seems to fall between "less than one
would hope" and "more than one would have thought possible".

John Roth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top