3

according to this post, I can enumerate instance variable by accessing str(self.__dict__), but I can't figure out how to do that with class variables.

Here's what I want to AVOID:

# I would like to print out class attributes by overriding `__str__` for that class.
class circle(object):
    radius = 3
    def __str__(self):     # I want to avoid instantiation like this.
        return str(circle.radius)

print(circle())    # I want to avoid instantiation. why can't I just print(circle)?
2
  • Related (or dupe?): stackoverflow.com/questions/4932438/… Commented Mar 27, 2018 at 9:14
  • @Aran-Fey I believe the answer presented in this question are much more clear than answers there. besides, the question is not exactly the same. Commented Mar 27, 2018 at 12:45

2 Answers 2

5

print(circle()) will call the __str__ method on an instance of circle.

class circle:
  def __str__(self):
    pass

as you can see here, you define __str__ on an instance of circle by using def on the parent class. Therefore you can override the __str__ method on the CLASS by using ITS parent.

 class circle(object):
     class __metaclass__(type):
         def __str__(cls):
             return str(cls.__dict__)
     radius = 3

Now, print circle will give you

{'__module__': '__main__', '__metaclass__': <class '__main__.__metaclass__'>, 'radius': 3, '__dict__': <attribute '__dict__' of 'circle' objects>, '__weakref__': <attribute '__weakref__' of 'circle' objects>, '__doc__': None}

EDIT python3 metaclass syntax

class meta(type):
  def __str__(cls):
    return str(cls.__dict__)

class circle(object, metaclass=meta):
  radius = 3
Sign up to request clarification or add additional context in comments.

1 Comment

Well at first I thought this is exactly what I want, but there's a minor problem: this code only seem to work on Python2, not Python3.
4

You can use the __dict__ member of the class object itself (possibly filtering out keys starting with __).

class circle(object):
    radius = 3

print({k: v for k,v in circle.__dict__.items() if not k.startswith('__')}) # prints {'radius': 3}

4 Comments

have you tried your code for yourself? it's throwing SyntaxError.
Eeh minor typo, coming from writing answers on the phone. There you go.
@AleksanderLidtke: indeed I already had it installed, and that's how I tried the fix out. :-) I was just too lazy to try it at first (the keyboard input in the REPL is positively horrible).

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.