#1. Initialize the instantiation property.
#可接受任意关键字参数 and assign them all as attributes to the instance. With **KW, you can also set properties by SetAttr (self, ' name ', ' xxx '), in addition to setting a property directly using Self.name = ' xxx '.
class Person (object):
def __init__ (self, name, gender, **kw):
self.name = name
Self.gender = Gender
For k,v in Kw.items ():
SetAttr (self, k, v)
xiaoming = person (' Xiao Ming ', ' Male ', birth= ' 1990-1-1 ', job= ' Student ')
print (xiaoming.name)
print (xiaoming.job)
#2. A private member of the class, "__score", which begins with a double underscore, cannot be accessed directly externally.
class Person1 (object):
def __init__ (self, Name, score):
self.name = name
self._gender = ' Male '
Self.__score = score
P1 = Person1 (' Bob ', ')
try:
print (p1.__ Score) #AttributeError: ' Person1 ' object has no attribute ' __score '
Except Attributeerror:
print (' Attrbuteerror ')
#3. Create a Class property. Instance properties Each instance owns and is independent of each other, and the class attribute has only one copy.
class Person2 (object):
address = ' Earth '
def __init__ (self, name):
self.name = name
print (person2.address)
P2 = Person2 (' Tom ')
print (person2.address)
#请给Person3类添加一个类属性count每创建一个实例count属性就加1这样就可以统计出一共创建了多少个Person3的实例.
class Person3 (object):
count = 0
def __init__ (self, name):
self.name = name
Person3.count + = 1
p3 = Person3 (' Alice ')
print (P3.count)
p3_1 = Person3 (' Tim ')
print (P3_1.count)
what happens to the class attribute and instance property name conflicts in #4. Python?
#请把上节的Person3类属性count改为__count, try again to access the property from the instance and class.
class Person4 (object):
__count = 0
def __init__ (self, name):
self.name = name
Person4.__count + = 1
print (Person4.__count)
P4 = Person4 (' Bob ')
p4_1 = Person4 (' Alice ')
Try:
print (Person4.__count) #AttributeError: Type object ' Person4 ' has no attribute ' __count '
except Attributeerror:
print (' Attributeerror ')
# #类属性的公开和私有, if there is no double underline, external can be called, if any, can only be used inside the class.
#5. The private properties of an instance are properties that begin with __ and cannot be accessed externally, what is the use of these attribute definitions?
#虽然私有属性无法从外部访问, however, can be accessed from within the class. In addition to defining the properties of an instance, you can define the methods of the instance.
#实例的方法就是在类中定义的函数, its first argument is always self, pointing to the instance itself that invokes the method, and the other arguments are exactly the same as a normal function:
class Person5 (object):
def __init__ (self, name):
self.__name = name
def get_name (self):
return Self.__name
P5 = Person5 (' Bob ')
print (P5.get_name ())
#任务: Please add a private attribute __score to the Person5 class, represent the fraction, and then add an instance method Get_grade (),
#能根据__score的值分别返回A-Excellent, B-pass, C-fail three.
class Person6 (object):
def __init__ (self, Name, score):
self.name = name
Self.__score = Score
def get_grade (self):
if Self.__score >=:
return "A-excellent"
elif self.__score >=:
return "b-passed"
return "c-failed"
p6_1 = Person6 (' Bob ', ")
p6_2 = Person6 (' Alice ',)
p6_3 = Person6 (' Tim ', ()
print (P6_1.get_grade ())
print (P6_2.get_grade ())
print (P6_3.get_grade ())
#6. Python methods are also attributes, because properties can be normal value objects, such as Str,int, can also be a method, or a function, you can see the results of the following code, think about why P6.get_grade is a function instead of a method:
class Person6 (object):
def __init__ (self, Name, score):
self.name = name
Self.score = Score
Self.get_grade = lambda: ' A '
P6 = Person6 (' Bob ', +)
print (p6.get_grade) #<function person6.__init__.<locals>.<lambda> at 0x000001d1244300d0>
print (P6.get_grade ()) #A
#直接把lambda函数赋值给self. Get_grade and binding methods differ, the function call does not need to pass in self, but the method call needs to pass in self.
#p6. Get_grade is a property, except that the property here is a function object.
#p6. Get_grade () is a method, and the preceding P6 is the object that invokes the method, that is, an instance, the whole sentence is an instance method
Python Object-Oriented Programming basics-initializing instances, class properties, methods