__author__ = 'mare' class MyClass: """ very simple class """ pass class Person: Name = "Default Name" def Print(self): print("My name is {0}".format( self.Name )) p1 = Person() p1.Print() p1.Name = "yuna" p1.Print() #My name is Default Name #My name is yuna #class object and instance object namespace class Person: name = "default name" p1 = Person(); p2 = Person(); print("p1's name: ", p1.name) print("p2's name: ", p2.name) #p1's name: default name #p2's name: default name p1.name = "yuna kim" print("p1's name: ", p1.name) print("p2's name: ", p2.name) #p1's name: yuna kim #p2's name: default name #new member create Person.title = "new title" print("p1's title: ", p1.title) print("p2's title: ", p2.title) #p1's title: new title # p2's title: new title print("Person's title: ", Person.title) #Person's title: new title p1.age = 20 print("p1's age", p1.age) #p1's age 20 #print("p2's age", p2.age) #error!!! #AttributeError: 'Person' object has no attribute 'age' #notice self keyword str = "Not class member" class GString: str = "" def set(self, msg): self.str = msg def Print(self): print(str) g = GString() g.set("First Message") g.Print() #Not class member #exception keyword, __class__ class Test: data = "Default" i2 = Test(); i1 = Test(); i1.__class__.data = "all data will be changed" print(i1.data) print(i2.data) #all data will be changed #all data will be changed i2.data = "only i2's data is changed" print(i1.data) print(i2.data) #all data will be changed #only i2's data is changed #relationship class object and instance object class Person: pass class Bird: pass class Student: pass p, s = Person(), Student() print( isinstance(p, Person) ) #True print( isinstance(s, Person) ) print( isinstance(p, object) ) print( isinstance(p, Bird) ) print( isinstance(int, object) ) #False #True #False #True #creator and deleter class MyClass: def __init__(self, value): self.Value = value print("Class is created! value = ", value) def __del__(self): print("Class is deleted") def foo(): d = MyClass(10) foo() #Class is created! value = 10 #Class is deleted #static method, class method class CounterManager: insCount = 0 def __init__(self): CounterManager.insCount += 1 def printInstanceCount(): print("Instance Count: ", CounterManager.insCount) a, b, c = CounterManager(), CounterManager(), CounterManager() CounterManager.printInstanceCount() #Instance Count: 3 #b.printInstanceCount() #error # rewrite rightly print("---") class CounterManager: insCount = 0 def __init__(self): CounterManager.insCount += 1 def staticPrintCount(): print("Instance Count: ", CounterManager.insCount) SPrintCount = staticmethod(staticPrintCount) def classPrintCount(cls): print("Instance Count: ", cls.insCount) CPrintCount = classmethod(classPrintCount) a, b, c = CounterManager(), CounterManager(), CounterManager() CounterManager.SPrintCount() b.SPrintCount() CounterManager.CPrintCount() b.CPrintCount() #Instance Count: 3 #Instance Count: 3 #Instance Count: 3 #Instance Count: 3 #operator re define class GString: def __init__(self, init=None): self.content = init def __add__(self, str): self.content += str; def __sub__(self, str): for i in str: self.content = self.content.replace(i, '') return GString(self.content) def __abs__(self): return GString( self.content.upper() ) def Remove(self, str): return self.__sub__(str) g = GString("abcdefgABCD") print(g.content) #abcdefgABCD g+"Apple_" print(g.content) #abcdefgABCDApple_ g -= "ade" print(g.content) #bcfgABCDAppl_ g -= "QQQ" print(g.content) #bcfgABCDAppl_ g = abs(g) print(g.content) #BCFGABCDAPPL_ #sequence operator redefine class Sequencer: def __init__(self, maxValue): self.maxValue = maxValue def __len__(self): return self.maxValue def __getitem__(self, index): if( 0 < index <= self.maxValue ): return index * 10 else: raise IndexError("Index out of range") def __contains__(self, item): return (0 < item <= self.maxValue) s = Sequencer(5) print( s[1] ) print( s[3] ) print( [s[i] for i in range(1,6) ] ) #10 #30 #[10, 20, 30, 40, 50] print( len(s) ) print( 3 in s ) print( 7 in s ) #5 #True #False #inheritance class Person: " parent class " def __init__(self, name, phoneNumber): self.Name = name self.PhoneNumber = phoneNumber def PrintInfo(self): print("Info(Name:{0}, Phone Number: {1}".format(self.Name, self.PhoneNumber)) def PrintPersonData(self): print("Person(Name:{0}, Phone Number:{1})".format(self.Name, self.PhoneNumber)) class Student(Person): "child class" def __init__(self, name, PhoneNumber, subject, studentID): self.Name = name self.PhoneNumber = PhoneNumber """ #call creator in parent class Person.__init__(self, name, phoneNumber) """ self.Subject = subject self.StudentID = studentID #method addition def PrintStudentData(self): print("Student(subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID)) p = Person("Derick", "010-123-4556") s = Student("Marry", "010-123-2222", "Computer science", "990999") print( p.__dict__ ) #{'Name': 'Derick', 'PhoneNumber': '010-123-4556'} print( s.__dict__ ) #{'Subject': 'Computer science', 'Name': 'Marry', 'PhoneNumber': '010-123-2222', 'StudentID': '990999'} print( issubclass(Student, Person) ) print( issubclass(Person, Student) ) print( issubclass(Person, Person) ) print( issubclass(Person, object)) print( issubclass(Student, object)) #True #False #True #True #True print( s.PrintStudentData() ) #Student(subject: Computer science, Student ID: 990999) print( dir(s) ) #Method overriding class Student2(Person): "child class" def __init__(self, name, PhoneNumber, subject, studentID): self.Name = name self.PhoneNumber = PhoneNumber self.Subject = subject self.StudentID = studentID #method addition def PrintStudentData(self): print("Student(subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID)) def PrintInfo(self): print("Info(Name:{0}, Phone Number: {1}".format(self.Name, self.PhoneNumber)) print("Student(subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID)) s = Student2("Marry", "010-123-2222", "Computer science", "990999") print( s.PrintInfo() ) #Info(Name:Marry, Phone Number: 010-123-2222 #Student(subject: Computer science, Student ID: 990999) #method extension class Student3(Person): "child class" def __init__(self, name, PhoneNumber, subject, studentID): self.Name = name self.PhoneNumber = PhoneNumber self.Subject = subject self.StudentID = studentID #method addition def PrintStudentData(self): print("Student(subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID)) def PrintInfo(self): Person.PrintInfo(self) print("Student(subject: {0}, Student ID: {1})".format(self.Subject, self.StudentID)) s = Student3("Marry", "010-123-2222", "Computer science", "990999") print( s.PrintInfo() ) #Info(Name:Marry, Phone Number: 010-123-2222 #Student(subject: Computer science, Student ID: 990999) #calss inheritance space and namespace class SuperClass: x=10 def printX(self): print(self.x) class SubClass(SuperClass): y=20 def printY(self): print(self.y) s = SubClass() s.a = 30 print( s.__dict__ ) #{'a': 30} print( s.y ) print( s.x ) #20 #10 class SuperClass: x=10 def printX(self): print(self.x) class SubClass(SuperClass): y=20 def printY(self): print(self.y) def printX(self): print("subClass:", self.x) s = SubClass() s.a = 30 s.x = 50 print("SuperClass: ", SuperClass.__dict__ ) print("SubClass: ", SubClass.__dict__ ) print("S: ", s.__dict__ ) """ SuperClass: {'__weakref__':, '__module__': '__main__', '__doc__': None, 'x': 10, '__dict__': , 'printX': } SubClass: {'__module__': '__main__', 'y': 20, '__doc__': None, 'printY': , 'printX': } S: {'a': 30, 'x': 50} """ #multi inheriance class Tiger: def Jump(self): print(" jump far like tiger") class Lion: def Bite(self): print("bite like lion") def Cry(self): print("zzzz") class Liger(Tiger, Lion): def Play(self): print("play with liger") l = Liger() l.Jump() l.Bite() l.Play() #jump far like tiger #bite like lion #play with liger l.Cry() #zzzz class Animal: def __init__(self): print("Animal __init()") class Tiger(Animal): def __init__(self): Animal.__init__(self) print("Tiger __init()") class Lion(Animal): def __init__(self): Animal.__init__(self) print("Lion __init()") class Liger(Tiger, Lion): def __init__(self): Tiger.__init__(self) Lion.__init__(self) print("liger __init()") l = Liger() #Animal __init() #Tiger __init() #Animal __init() #Lion __init() #liger __init() # rewrite by super init class Animal2: def __init__(): print("Animal __init()") class Tiger2(Animal): def __init__(self): super().__init__() print("Tiger __init()") class Lion2(Animal): def __init__(self): super().__init__() print("Lion __init()") class Liger2(Tiger2, Lion2): def __init__(self): super().__init__() print("liger __init()") print("---") l = Liger2() #Animal __init() #Lion __init() #Tiger __init() #liger __init()
1/27/2014
(Python Study) about class (example source code)
Subscribe to:
Post Comments (Atom)
-
Logistic Classifier The logistic classifier is similar to equation of the plane. W is weight vector, X is input vector and y is output...
-
As you can see in the following video, I created a class that stitching n cameras in real time. https://www.youtube.com/user/feelmare/sear...
-
fig 1. Left: set 4 points (Left Top, Right Top, Right Bottom, Left Bottom), right:warped image to (0,0) (300,0), (300,300), (0,300) Fi...
-
Image size of origin is 320*240. Processing time is 30.96 second took. The result of stitching The resul...
-
* Introduction - The solution shows panorama image from multi images. The panorama images is processing by real-time stitching algorithm...
-
Created Date : 2011.10 Language : C/C++ Tool : Microsoft Visual C++ 2008 Library & Utilized : OpenCV 2.3 Reference : SIFT referenc...
-
OpenCV has AdaBoost algorithm function. And gpu version also is provided. For using detection, we prepare the trained xml file. Although...
-
In the YUV color format, Y is bright information, U is blue color area, V is red color area. Show the below picture. The picture is u-v col...
-
I once wrote the following article. http://study.marearts.com/2014/04/opencv-study-mat-point-access-method.html This article is a sample c...
-
Created Date : 2011.8 Language : Matlab Tool : Matlab 2010 Library & Utilized : - Reference : Multiple View Geometry (Hartly and Z...
No comments:
Post a Comment