Latest |Kites |Pictures |Programming |Life
[filed under Programming]Python static method staticmethod

Tagged: @staticmethod, staticmethod, class, def, static, static method, static variable, python, def, __init__, self

OK, I'm always forgetting how to add static methods to Python classes, so:

class C:
    val = 99

    def __init__(self,x):
        self.x = x

    @staticmethod
    def set_val(val):
        C.val = val

    @staticmethod
    def get_val():
        return C.val


a = C('a')
b = C('b')

print C.get_val()
print '-'*5

a.set_val(88)
print C.get_val()
print a.get_val(), a.x
print b.get_val(), b.x
print '-'*5

b.set_val(69)
print C.get_val()
print a.get_val(), a.x
print b.get_val(), b.x

 

>>>
99
-----
88
88 a
88 b
-----
69
69 a
69 b

Job done.

 

29th of July, 2008@10:00:05 AM
add a comment, permanent link to article

Comments

Check this if you are a human being. Thanks. (I'm trying to reduce my comment spam :-)

Comment

Server Grind [0.0024 seconds]