conditionally creating functions within a class?

Discussion in 'Python' started by kaens, May 26, 2007.

  1. kaens

    kaens Guest

    So, I have a class that has to retrieve some data from either xml or
    an sql database.
    This isn't a problem, but I was thinking "hey, it would be cool if I
    could just not define the functions for say xml if I'm using sql", so
    I did some fiddling around with the interpreter.

    First, I try conditionally creating a function, period:

    a = 'a'

    if(a == 'a')
    def b:
    print "hello"
    else:
    def c:
    print "goodbye"

    this works fine. b is defined, c is not. change the value of a and b
    gets defined and not c (sorry for the one-letter variables here, but
    for these little examples I don't think they detract much)

    then I try doing this within a function:

    class test:
    def __init__(self,which):
    self.which = which

    if(self.which == 'a'):
    def b:
    print "hello"
    else:
    def c:
    print "goodbye"

    tester = test('a')
    tester.b()

    This doesn't "compile", says "Name 'self' is not defined". I assume
    this is because of scope, something like it hasn't made the object
    yet, so there is no self attribute. . . but I thought that python
    wouldn't even bother reading that class statement until I tried to
    make a test object, and that it would do the __init__ function before
    anything else, so I'm a bit fuzzy here.

    Next I try creating the functions through functions:

    class test:
    def __init__(self, which):
    self.which = which
    self.chooser()

    def chooser(self):
    if( self.which == 'a'):
    def b(self):
    print "hello"
    else:
    def c(self):
    print "goodbye"

    tester = test('a')
    tester.b()

    this tells me "instance has no attribute b.

    I'm pretty sure this is all a scoping error of some sort (I could be
    wrong), but I don't have my head wrapped around it at all. Anyone with
    more knowledge care to explain what's going on?

    Also, would there be a way to conditionally create functions in a
    class? It doesn't really matter, but it'd be nice if I weren't
    creating functions that I absolutely will not need for certain
    instances at runtime
     
    kaens, May 26, 2007
    #1
    1. Advertising

  2. kaens

    Dan Bishop Guest

    On May 25, 7:44 pm, kaens <> wrote:
    > So, I have a class that has to retrieve some data from either xml or
    > an sql database.
    > This isn't a problem, but I was thinking "hey, it would be cool if I
    > could just not define the functions for say xml if I'm using sql", so
    > I did some fiddling around with the interpreter.
    >
    > First, I try conditionally creating a function, period:
    >
    > a = 'a'
    >
    > if(a == 'a')
    > def b:
    > print "hello"
    > else:
    > def c:
    > print "goodbye"
    >
    > this works fine. b is defined, c is not. change the value of a and b
    > gets defined and not c (sorry for the one-letter variables here, but
    > for these little examples I don't think they detract much)
    >
    > then I try doing this within a function:
    >
    > class test:
    > def __init__(self,which):
    > self.which = which
    >
    > if(self.which == 'a'):
    > def b:
    > print "hello"
    > else:
    > def c:
    > print "goodbye"
    >
    > tester = test('a')
    > tester.b()
    > ...



    class Test:
    def __init__(self, which):
    self.which = which
    if which == 'a':
    self.__class__ = TestB
    else:
    self.__class__ = TestC

    class TestB(Test):
    def b(self):
    print "hello"

    class TestC(Test):
    def c(self):
    print "goodbye"
     
    Dan Bishop, May 26, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spartanicus
    Replies:
    3
    Views:
    461
    Spartanicus
    Sep 30, 2005
  2. Xiangliang Meng
    Replies:
    1
    Views:
    1,661
    Victor Bazarov
    Jun 21, 2004
  3. Mike
    Replies:
    1
    Views:
    673
  4. Replies:
    2
    Views:
    366
    Dave Thompson
    Feb 27, 2006
  5. Alfredo Garcia lopez
    Replies:
    1
    Views:
    196
    Alfredo Garcia lopez
    Oct 22, 2008
Loading...

Share This Page