Re: polymorphism in python

From: KefX (keflimarcusx_at_aol.comNOSPAM)
Date: 11/26/03


Date: 26 Nov 2003 22:52:49 GMT


>f your intent is that you could call foo(4) or foo('4'), you could just
>write:
>
>def foo (x):
> theRealX = int (x)
>
>or you could maybe even do:
>
>def foo (x):
> if type(x) == StringType:
> do string stuff
> else:
> do integer stuff
>
>but if you're trying to do either of the above, I suspect you're trying
>to write C++ in Python (which is as bad, if not worse, as trying to
>write Fortran in Python).

The second example should use the expression:
isinstance(x, str)

and not this:
type(x) == StringType

But as you said, it's best to avoid doing it at all ;)

- Kef



Relevant Pages

  • Re: How naive is Python?
    ... is the current Python system? ... All of those just move around pointers to the same string. ... s = "".join (temp) ...
    (comp.lang.python)
  • Re: Question about how ruby sources is parsed
    ... I can't imaging that it's good to do thing that way. ... class String0 < String ...
    (comp.lang.ruby)
  • Re: Question about how ruby sources is parsed
    ... On Feb 27, 2008, at 07:19 AM, ThoML wrote: ... class String0 < String ... Object.send:remove_const,:String ...
    (comp.lang.ruby)
  • Question about how ruby sources is parsed
    ... class String0 < String ... Object.send:remove_const,:String ... new String class somehow? ...
    (comp.lang.ruby)
  • Re: C or C++
    ... people who know C tend to avoid using the C++ standard ... C string handling. ... programmer. ... using the preprocessor for integer constants should be ...
    (comp.os.linux.development.apps)