Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45917

Re: Polymoprhism question

From Neil Cerutti <neilc@norwich.edu>
Newsgroups comp.lang.python
Subject Re: Polymoprhism question
Date 2013-05-24 20:23 +0000
Organization Norwich University
Message-ID <b0a0ikFo8f2U1@mid.individual.net> (permalink)
References <c89c2c9a-4896-4573-90f4-225a1ce6d950@googlegroups.com> <3f9acff7-151b-4aa4-8b59-fcddc2755691@googlegroups.com>

Show all headers | View raw


On 2013-05-24, RVic <rvince99@gmail.com> wrote:
> Thanks Steven,
>
> Yes, I see Python isn't going to do this very well, from what I
> can understand.
>
> Lets say I have a type of class, and this type of class will
> always have two methods, in() and out().
>
> Here is, essentially, what I am trying to do, but I don't know
> if this will make sense to you or if it is really doable in
> Python:  #thanks, RVic
>
> import sys
> argv = sys.argv[1:] 
> ClassIamInstantiating = argv
> ClassIamInstantiating.in("something")
> x = ClassIamInstantiating.out()

This is pretty easy in Python using the __name__ attribute.

import sys

class A:
 def in(self):
   print("A in")
 def out(self):
   print("A out")

class B:
 def in(self):
   print("B in")
 def out(self):
   print("B out")

classes = {cls.__name__: cls for cls in (A, B)}

ArgType = classes[sys.agrv[1]]

arg = ArgType()

arg.in("test")
arg.out("test")

-- 
Neil Cerutti

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Polymoprhism question RVic <rvince99@gmail.com> - 2013-05-24 04:40 -0700
  Re: Polymoprhism question Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-05-24 12:10 +0000
  Re: Polymoprhism question RVic <rvince99@gmail.com> - 2013-05-24 12:19 -0700
    Re: Polymoprhism question Neil Cerutti <neilc@norwich.edu> - 2013-05-24 20:23 +0000

csiph-web