Polymoprhism question
Neil Cerutti
neilc at norwich.edu
Fri May 24 16:23:48 EDT 2013
On 2013-05-24, RVic <rvince99 at 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
More information about the Python-list
mailing list