0

OK, so heres what I'm trying to do:

    class MyClass(object):      #Class I want to access
    """MyClass, base class for my class"""
    def __init__(self, numberOfApples):
        numberOfApples = 12     #Attribute I plan to use

myClassString = "MyClass"

#What I'm trying to do:

if myClassString.numberOfApples >= 12:   """myClassString is in place of the real class name, MyClass"""
    print "Wow, you got alot of apples!"

And what I'm trying to do is seeing if it is possible to reference an attribute of a class by using a string in place of the actual class name. Is this possible? If not, what's the closest thing I can get? Thanks.

said
  • 474
  • 4
  • 8
  • 18
  • closet and best would be create a dictionary, mapping strings to classes. – ZdaR Jun 05 '15 at 00:21
  • Just create the instance of the class. Otherwise, myClassString is a string – Zizouz212 Jun 05 '15 at 00:24
  • The marked duplicate will show you how to retrieve attributes using a string. You can theoretically acccomplish something very similiar by using `locals()` to retrieve things in the namespace, but realistically that's usually a design anti-pattern. – g.d.d.c Jun 05 '15 at 00:25

0 Answers0