top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Polymoprhism question in python

0 votes
21 views

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 but I don't know if there is such a thing in Python) then that class of that name, somehow gets instantiated 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?

posted May 24, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

1 Answer

+1 vote
 
Best answer

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
answer May 24, 2013 by anonymous
Similar Questions
+2 votes

It is difficult to install numpy package for my PC Windows 7, 64-bit OS. In the end, I install Enthought Canopy, which is recommended on line because it does install numpy automatically. Now, I can test it with

import numpy

it succeeds. On http://wiki.scipy.org/Cookbook, it shows some interesting code example snippet, such as Cookbook / ParticleFilter, Markov chain etc.

I don't know how I can access these code examples, because I don't know where Enthought Canopy installs these package.

Could you help me on using numpy examples?

0 votes

I am extending a parser and need to create many classes that are all subclassed from the same object (defined in an external library). When my module is loaded I need all the classes to be created with a particular name but the behavior is all the same. Currently I have a bunch of lines like this:

 class Vspace(Base.Command): pass
 class Boldpath(Base.Command): pass

There are a bunch of lines like that. Is there a better way? Something like

 newclasses = ['Vspace', 'Boldpath', ... ]
 for name in newclasses:
 tmp = type(name, (Base.Command,) {})
 tmp.__name__ = name

Is there a more pythonic way?

+3 votes

I'm looking for a simple Address Book program written in Python, I've looked at addressbook 1.0.1 from PyPi but it doesn't quite do what I want. I may have to start with this and modify it but maybe there's something better out there.

In particular I want some sort of import/export ability and I'd really like some more configurability.

Any ideas anyone?

+2 votes

I have a pile of C code that I wrote that I want to interface to via the ctypes module ( https://docs.python.org/3/library/ctypes.html ).

The C code uses the Boehm-Demers-Weiser garbage collector ( http://www.hboehm.info/gc/ ) for all of its memory management. What I want to know is, who owns allocated memory? That is, if my C code allocates memory via GC_MALLOC() (the standard call for allocating memory in the garbage collector), and I access some object via ctypes in python, will the python garbage collector assume that it owns it and attempt to dispose of it when it goes out of scope?

Ideally, the memory is owned by the side that created it, with the other side simply referencing it, but I want to be sure before I invest a lot of time interfacing the two sides together.


Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#470/147, 3rd Floor, 5th Main,
HSR Layout Sector 7,
Bangalore - 560102,
Karnataka INDIA.
QUERY HOME
...