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


Groups > comp.lang.python > #45877 > unrolled thread

Polymoprhism question

Started byRVic <rvince99@gmail.com>
First post2013-05-24 04:40 -0700
Last post2013-05-24 20:23 +0000
Articles 4 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#45877 — Polymoprhism question

FromRVic <rvince99@gmail.com>
Date2013-05-24 04:40 -0700
SubjectPolymoprhism question
Message-ID<c89c2c9a-4896-4573-90f4-225a1ce6d950@googlegroups.com>
I'm trying to figure out (or find an example) of polymorphism whereby I pass a commandline argument (a string) which comports to a class (in java, you would say that it comports to a given interface bu I don't know if there is such a thing in Python) then that class of that name, somehow gets intantiated from that string. This way, I can have similar classes, but have my program use various ones by simply changing the commandline argument.

Can anyone show me how this might be done in Python? Thanks.

-RVic

[toc] | [next] | [standalone]


#45879

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2013-05-24 12:10 +0000
Message-ID<519f58a7$0$6599$c3e8da3$5496439d@news.astraweb.com>
In reply to#45877
On Fri, 24 May 2013 04:40:22 -0700, RVic wrote:

> I'm trying to figure out (or find an example) of polymorphism whereby I
> pass a commandline argument (a string) which comports to a class (in
> java, you would say that it comports to a given interface bu I don't
> know if there is such a thing in Python) then that class of that name,
> somehow gets intantiated from that string. This way, I can have similar
> classes, but have my program use various ones by simply changing the
> commandline argument.
> 
> Can anyone show me how this might be done in Python? Thanks.


I'm not 100% sure I understand what you want, but my guess is you want 
something like this:


# A toy class.
class AClass(object):
    def __init__(self, astring):
        self.astring = astring
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.astring)

# And some variations.
class BClass(AClass):
    pass

class CClass(AClass):
    pass


# Build a dispatch table, mapping the class name to the class itself.
TABLE = {}
for cls in (AClass, BClass, CClass):
    TABLE[cls.__name__] = cls


# Get the name of the class, and an argument, from the command line.
# Or from the user. Any source of two strings will do.
# Data validation is left as an exercise.
import sys
argv = sys.argv[1:]
if not argv:
    name = raw_input("Name of the class to use? ")
    arg = raw_input("And the argument to use? ")
    argv = [name, arg]


# Instantiate.
instance = TABLE[argv[0]](argv[1])
print instance


-- 
Steven

[toc] | [prev] | [next] | [standalone]


#45911

FromRVic <rvince99@gmail.com>
Date2013-05-24 12:19 -0700
Message-ID<3f9acff7-151b-4aa4-8b59-fcddc2755691@googlegroups.com>
In reply to#45877
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()

[toc] | [prev] | [next] | [standalone]


#45917

FromNeil Cerutti <neilc@norwich.edu>
Date2013-05-24 20:23 +0000
Message-ID<b0a0ikFo8f2U1@mid.individual.net>
In reply to#45911
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web