Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #45917
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Neil Cerutti <neilc@norwich.edu> |
| Newsgroups | comp.lang.python |
| Subject | Re: Polymoprhism question |
| Date | 24 May 2013 20:23:48 GMT |
| Organization | Norwich University |
| Lines | 46 |
| Message-ID | <b0a0ikFo8f2U1@mid.individual.net> (permalink) |
| References | <c89c2c9a-4896-4573-90f4-225a1ce6d950@googlegroups.com> <3f9acff7-151b-4aa4-8b59-fcddc2755691@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net qFBiyY0ufiYUklKnuP0vdQ3p5jcqymuAtdnci/L9JAqc7HNxcl |
| Cancel-Lock | sha1:rCriamxiDOFImSMe1+aAPXEHQA4= |
| User-Agent | slrn/0.9.9p1/mm/ao (Win32) |
| Xref | csiph.com comp.lang.python:45917 |
Show key headers only | 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 | Next — Previous in thread | Find similar | Unroll 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