Petter Petter Petter Petter - 1 year ago 66
Python Question

Listing all the variables in a Python class

I've made a class, and after the program has finished running, I want to list all the variables I have kinda like so

var1: val
var2: val

my class looks like this

class character:

name = 'Player1'
level = 1
str = 100
con = 80
sta = 150
critchance = 0.1
critdmg = 0.5

# for battle
hp = con * con
thoughness = math.sqrt(sta)

Answer Source

Is something like this what you are looking for?

for var in dir(character):
    if var[:2] != '__':
        print '%s:' % var, getattr(character, var)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download