For reprinting, please indicate the source:
http://www.cnblogs.com/why168888/p/6416148.html
This article is from: [Edwin Blog Garden]
Basic Object-Oriented Programming (Advanced 4)
1. python's Object-Oriented Programming
Everything is an object, because I learned Java object-oriented programming ideas, Python is the same, so simply write this section.
What is Object-Oriented Programming
- Object-oriented programming is a programming paradigm
- Think of programs as calls between different objects
- Establishing an Object Model for the Present World
Basic Ideas of Object-Oriented Programming
- Class and real column
- Classes are used to define abstract types
- Instances are created based on class definitions
2. python defines classes and creates instances
# -*- coding:utf-8 -*- class Person(object): pass xiaoming = Person() xiaohong = Person() print xiaoming print xiaohong print xiaoming == xiaohong
3. Creating instance attributes in Python
class Person(object): pass p1 = Person() p1.name = 'Bart' p2 = Person() p2.name = 'Adam' p3 = Person() p3.name = 'Lisa' L1 = [p1, p2, p3] L2 = sorted(L1, lambda p1, p2: cmp(p1.name, p2.name)) print L2[0].name print L2[1].name print L2[2].name
4. Initialize instance properties in Python
When defining the Person class, you can add a special _init_() method to the Person class. When creating an instance, the init() method is called automatically. We can add the following attributes to each instance.
class Person(object): def __init__(self, name, gender, birth, **kw): self.name = name self.gender = gender self.birth = birth for k, v in kw.iteritems(): setattr(self, k, v) xiaoming = Person('Xiao Ming', 'Male', '1990-1-1', job='Student') print xiaoming.name print xiaoming.job
5. Access restrictions in Python
Python controls the permissions of attributes through the name of attributes. If an attribute begins with a double underscore (_), it cannot be accessed externally.
class Person(object): def __init__(self, name, score): self.name = name self.__score = score p = Person('Bob', 59) print p.name print p.__score
6. Creating class attributes in Python
Each instance has its own, independent of each other, while class attributes have and only have one.
class Person(object): count = 0 def __init__(self, name): Person.count = Person.count + 1 self.name = name p1 = Person('Bob') print Person.count # 1 p2 = Person('Alice') print Person.count # 2 p3 = Person('Tim') print Person.count # 3
7. What about name conflicts between class attributes and instance attributes in Python
class Person(object): __count = 0 def __init__(self, name): Person.__count = Person.__count + 1 self.name = name print Person.__count p1 = Person('Bob') p2 = Person('Alice') print Person.__count
8. Defining instance methods in Python
class Person(object): def __init__(self, name, score): self.__name = name self.__score = score def get_grade(self): if self.__score >= 80: return 'A' if self.__score >= 60: return 'B' return 'C' p1 = Person('Bob', 90) p2 = Person('Alice', 65) p3 = Person('Tim', 48) print p1.get_grade() print p2.get_grade() print p3.get_grade()
9. Methods in Python are also attributes
class Person(object): def __init__(self, name, score): self.name = name self.score = score self.get_grade = lambda: 'A' p1 = Person('Bob', 90) print p1.get_grade print p1.get_grade()
10. Defining class methods in Python
Similar to attributes, methods are divided into instance methods and class methods.
class Person(object): __count = 0 @classmethod def how_many(cls): return cls.__count def __init__(self, name): self.name = name Person.__count = Person.__count + 1 print Person.how_many() p1 = Person('Bob') print Person.how_many()