Python Tutorial
class ClassName: statement - 1 . . statement - N
object_name.attribute_name
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; def show_message(self): return "Welcom to python class!"; # attribute references print TestClass.variable1; print TestClass.show_message; print TestClass.__doc__;Output:
$ python test_class.py 100 <unbound method TestClass.show_message> Test Class
instance_name = TestClass()
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; def show_message(self): return "Welcome to python class!"; obj = TestClass(); print obj.variable1; print obj.show_message(); print obj.__doc__;Output;
$ python test_class.py 100 Welcome to python class! Test Classcreates a new instance of the class and assigns this object to the local variable 'instance_name'.
def __init__(self): self.data = []
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; def __init__(self, msg): self.msg = msg; def show_message(self): return self.msg; obj = TestClass("Welcome to python class!"); print obj.variable1; print obj.show_message(); print obj.__doc__;Output:
$ python test_class.py 100 Welcome to python class! Test Class
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; def __init__(self, msg): self.msg = msg; def show_message(self): return self.msg; obj = TestClass("Welcome to python class!"); sm = obj.show_message; print sm();Output:
Welcome to python class!here obj.show_message is the method object. sm stores the method object and can be called at later time.
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; def __init__(self, msg): self.msg = msg; def show_message(self): return self.msg; obj = TestClass("Welcome to python class!"); print TestClass.show_message(obj);here obj.show_message() is equivalent to TestClass.show_message(obj).
#!/usr/bin/pyhton class TestClass: """Test Class""" variable1 = 100; # class variable def __init__(self, msg): self.msg = msg; # instance variable obj = TestClass("Instance object1"); obj1 = TestClass("Instance object2"); print "obj1 - variable1: ", obj.variable1, " and msg: ", obj.msg; print "obj2 - variable2: ", obj1.variable1, " and msg: ", obj1.msg;Output:
$ python test_class.py obj1 - variable1: 100 and msg: Instance object1 obj2 - variable2: 100 and msg: Instance object2
#!/usr/bin/pyhton class TestClass: """Test Class""" instances = []; # class variable def __init__(self, msg, ins): self.msg = msg; # instance variable self.instances.append(ins); obj = TestClass("Instance object1", "instance 1"); obj1 = TestClass("Instance object2", "instance 2"); print "obj1 - instances: ", obj.instances, " and msg: ", obj.msg; print "obj2 - instances: ", obj1.instances, " and msg: ", obj1.msg;Output:
$ python test_class.py obj1 - instances: ['instance 1', 'instance 2'] and msg: Instance object1 obj2 - instances: ['instance 1', 'instance 2'] and msg: Instance object2
#!/usr/bin/pyhton # Function defined outside the class def show_message(self): return self.msg; class TestClass: """Test Class""" def __init__(self, msg): self.msg = msg; def get_length(self): return len(self.msg); sm = show_message gl = get_length obj = TestClass("Instance object"); obj1 = TestClass("Instance object 1"); print "obj1 - msg length: ", obj.gl(), " and msg: ", obj.sm(); print "obj2 - msg length: ", obj1.gl(), " and msg: ", obj1.sm();Output:
$ python test_class.py obj1 - msg length: 15 and msg: Instance object obj2 - msg length: 17 and msg: Instance object 1
#!/usr/bin/pyhton class TestClass: """Test Class""" def __init__(self, msg): self.msg = msg; def get_length(self): return len(self.msg); def show_message(self): return self.msg + "length: " + str(self.get_length()); obj = TestClass("Instance object"); obj1 = TestClass("Instance object 1"); print "obj1 - msg: ", obj.show_message(); print "obj2 - msg: ", obj1.show_message();Output:
$ python test_class.py obj1 - msg: Instance objectlength: 15 obj2 - msg: Instance object 1length: 17
Python Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page