Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language

Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Python Programming Language

Something like the getattr() trick.

I'm working with the following class heirarchy (I've snipped out the code
from the classes):

class Vuln:
        def __init__(self, url):

        def _parse(self):

        def get_link(self):

class VulnInfo(Vuln):

class VulnDiscuss(Vuln):

def main(url):
        vuln_class = ['Info', 'Discuss']
        vuln = Vuln(url)
        for link in vuln.get_link():
                i = VulnInfo(link)
                d = VulnDiscuss(link)

Is there a way to get references to VulnInfo and VulnDiscuss objects using
something like the getattr trick? For example, something like:

        for _class in vuln_class:
                class_obj = getattr('Vuln%s' % (_class,) ..)
                a = class_obj(link)

getattr() takes an object as its first argument. I can't seem to figure
out how to make it work here.

Ayaz Ahmed Khan

A witty saying proves nothing, but saying something pointless gets
people's attention.

On Feb 10, 3:34 pm, Ayaz Ahmed Khan <a@dev.slash.null> wrote:

eval('Vuln' + _class)
Vuln.Discuss = VulnDiscuss
getattr(Vuln, _class)
This is a really common question. What you really need here is to
lookup some value (one of the two classes) by a key (the names of the
classes). Does that sound like something familiar? You seem to need a
dictionary, where you think you want lookup some global objects by

Alternatively, if you use new-style classes (by`inheriting the object
class in your base class), you could perhaps add a method such as
getSubClass() like:

class Vuln(object):
  def getSubClass(cls, name):
    for c in cls.__subclasses__():
      if c.__name__ == name:
        return c
    raise ValueError("No subclass named '%s' found." % name)

Of course, this only makes sense if you needs dont extend outside the
pattern of looking up subclasses by name. It has the advantage that
you can also put the subclasses in other modules and still look them
up from one place.

On 2/10/07, Ayaz Ahmed Khan <a@dev.slash.null> wrote:

Read my blog! I depend on your acceptance of my opinion! I am interesting!
On 2/10/07, Ayaz Ahmed Khan <a@dev.slash.null> wrote:

In addition to what other people has suggested:

- Use some kind of registry:
register("Vuln", Vuln, VulnInfo, VulnDiscuss)
(a dictionary would do) and then look up by name

- If all three classes are contained inside the same module, look up them  
by name into globals: InfoClass = globals()["%sInfo" %  
(assuming self is an instance of Vuln, this would give you the VulnInfo  

- Replace your line: vuln_class = ['Info', 'Discuss']
with vuln_class = [VulnInfo, VulnDiscuss]

As you see, there are many ways to do that - choose what better fits your  

Gabriel Genellina

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc