1

I have this python code which set A or B in a class. I want to print what this class receives:

if options.A:
   print "setting A"
   class TmpClass(A): pass
else:
   print "nothing set in the command line => setting to B"
   class TmpClass(B): pass

print "selected=",TmpClass

I want to see either A or B in the output but I see:

selected= TmpClass
3
  • 1
    This looks really weird. Are you quite sure you know what you are doing? If not, please research OOP again, because apparently you did not understand the concept. Commented Nov 22, 2011 at 9:16
  • Do you have two classes A and B? Because what you are doing is creating a new class TmpClass that inherits from either A or B. Commented Nov 22, 2011 at 9:25
  • Sorry.... That question does not represent my problem. I will try to edit it soon. Commented Nov 22, 2011 at 9:27

4 Answers 4

3

What your code is doing, translated in English is:

if option.A has a value that evaluates to True:
    define an empty class called "TmpClass" that inherits from the object called "A"
otherwise:
    define an empty class called "TmpClass" that inherits from the object called "B"

Now, if what the code is actually doing is indeed what you intended, my guess is that what you wanted could be to know whether your class is A or B -based... If I am right, then the line you want to have at the end is:

print('TmpClass inherits from : %s' % TmpClass.__bases__)

HTH!

Sign up to request clarification or add additional context in comments.

1 Comment

brilliant... that was exactly what I meant :)
1

You can assign classes to a variable, without creating instances of them:

if options.A:
   print "setting A"
   TmpClass = A
else:
   print "nothing set in the command line => setting to B"
   TmpClass = B

print "selected=",TmpClass

Comments

1

You might look at using isinstance(). For example:

class MyBaseClass():
    def __init__(self):
        self.cType = 'Base'

    def whichClass(self):
        print 'Class type = {0}'.format(self.cType)

        if isinstance(self,DerivedClassA):
            print 'Derived Class A'
        elif isinstance(self,DerivedClassB):
            print 'Derived Class B'
        elif isinstance(self,MyBaseClass):
            print 'Not A or B'
        else:
            print 'Unknown Class'

class DerivedClassA(MyBaseClass):
    def __init__(self):
        self.cType = 'Class A'

class DerivedClassB(MyBaseClass):
    def __init__(self):
        self.cType = 'Class B'


Then Run:

base = MyBaseClass()
a = DerivedClassA()
b = DerivedClassB()
a.whichClass()
>> Class type = Class A
>> Derived Class A
b.whichClass()
>> Class type = Class B
>> Derived Class B
base.whichClass()
>> Class type = Base
>> Not A or B

Comments

0

A and B are formal arguments passed to the class here, not actual values of A and B.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.