Python-基础(5)-Classes

类 Classes

类的定义 Class Definition

Python是一种面向对象的编程语言。


几乎Python语言中的一切都可以看作一个有自己属性和方法的对象。


类Class就像一个对象构造器,或者打印机用于创建对象。
A Class is like an object constructor, or a “blueprint” for creating objects..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def test_class():
"""Class definition."""

# Class definitions, like function definitions (def statements) must be executed before they
# have any effect. (You could conceivably place a class definition in a branch of an if
# statement, or inside a function.)

class GreetingClass:
"""Example of the class definition
This class contains two public methods and doesn't contain constructor.
"""
name = 'Bisrat'

def say_hello(self):
"""Class method."""
# The self parameter is a reference to the class itself, and is used to access variables
# that belongs to the class. It does not have to be named self , you can call it
# whatever you like, but it has to be the first parameter of any function in the class.
return 'Hello ' + self.name

def say_goodbye(self):
"""Class method."""
return 'Goodbye ' + self.name

# When a class definition is entered, a new namespace is created, and used as the local scope —
# thus, all assignments to local variables go into this new namespace. In particular, function
# definitions bind the name of the new function here.

# Class instantiation uses function notation. Just pretend that the class object is a
# parameterless function that returns a new instance of the class. For example the following
# code will creates a new instance of the class and assigns this object to the local variable.
# 创建类实例,greeter为实例对象的引用
greeter = GreetingClass()

assert greeter.say_hello() == 'Hello Bisrat'
assert greeter.say_goodbye() == 'Goodbye Bisrat'

>>> print(type(content))
<class 'str'>



类对象 Class Objects

Python是一种面向对象的编程语言。


几乎Python语言中的一切都可以看作一个有自己属性和方法的对象。


所以。。。类本身也是一种对象(Objects)
A Class is like an object constructor, or a “blueprint” for creating objects..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

def test_class_objects():
"""Class Objects.
Class objects support two kinds of operations:
- attribute references
- instantiation.
"""

# ATTRIBUTE REFERENCES use the standard syntax used for all attribute references in
# Python: obj.name. Valid attribute names are all the names that were in the class’s namespace
# when the class object was created. For class MyCounter the following references are valid
# attribute references:

class ComplexNumber:
"""Example of the complex numbers class"""

real = 0
imaginary = 0

def get_real(self):
"""Return real part of complex number."""
return self.real

def get_imaginary(self):
"""Return imaginary part of complex number."""
return self.imaginary

# 并没有创建类实例instance,没有调用类的构造方法。
# 直接通过类引用,调用类对象class object
assert ComplexNumber.real == 0

# __doc__ is also a valid attribute, returning the docstring belonging to the class
assert ComplexNumber.__doc__ == 'Example of the complex numbers class'

# Class attributes can also be assigned to, so you can change the value of
# ComplexNumber.counter by assignment.
ComplexNumber.real = 10
assert ComplexNumber.real == 10

# CLASS INSTANTIATION uses function notation. Just pretend that the class object is a
# parameterless function that returns a new instance of the class. For example
# (assuming the above class):
# 创建实例对象complex_number, 此时创建对象是根据已经改变过的类对象作为创建的模板。
complex_number = ComplexNumber()

assert complex_number.real == 10
assert complex_number.get_real() == 10

# Let's change counter default value back.
ComplexNumber.real = 10
assert ComplexNumber.real == 10

# The instantiation operation (“calling” a class object) creates an empty object. Many classes
# like to create objects with instances customized to a specific initial state. Therefore a
# class may define a special method named __init__(), like this:

class ComplexNumberWithConstructor:
"""Example of the class with constructor"""
def __init__(self, real_part, imaginary_part):
self.real = real_part
self.imaginary = imaginary_part

def get_real(self):
"""Return real part of complex number."""
return self.real

def get_imaginary(self):
"""Return imaginary part of complex number."""
return self.imaginary

complex_number = ComplexNumberWithConstructor(3.0, -4.5)
assert complex_number.real, complex_number.imaginary == (3.0, -4.5)


实例对象 Instance Object

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def test_instance_objects():
# DATA ATTRIBUTES need not be declared; like local variables, they spring into existence when
# they are first assigned to. For example, if x is the instance of MyCounter created above,
# the following piece of code will print the value 16, without leaving a trace.

# pylint: disable=too-few-public-methods
class DummyClass:
pass

dummy_instance = DummyClass()

# pylint: disable=attribute-defined-outside-init
dummy_instance.temporary_attribute = 1
assert dummy_instance.temporary_attribute == 1
del dummy_instance.temporary_attribute


方法对象 Method Objects

  • 实例方法(第一个参数是self,自动传递实例对象)
  • 静态方法(没有self参数)
  • 类方法(第一个参数是cls类对象)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class MyCounter:
"""A simple example of the counter class"""
counter = 10

def get_counter(self):
"""Return the counter"""
return self.counter

def increment_counter(self):
"""Increment the counter"""
self.counter += 1
return self.counter


def test_method_objects():
"""Method Objects."""

# object types can have methods as well. For example, list objects have methods called append,

counter = MyCounter()
assert counter.get_counter() == 10

get_counter = counter.get_counter
assert get_counter() == 10



assert counter.get_counter() == 10
assert MyCounter.get_counter(counter) == 10


继承 Inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# pylint: disable=too-few-public-methods
class Person:
"""Example of the base class"""
def __init__(self, name):
self.name = name

def get_name(self):
"""Get person name"""
return self.name


# The syntax for a derived class definition looks like this.
class Employee(Person):

def __init__(self, name, staff_id):
Person.__init__(self, name)
# You may also use super() here in order to avoid explicit using of parent class name:
# >>> super().__init__(name)
self.staff_id = staff_id

def get_full_id(self):
"""Get full employee id"""
return self.get_name() + ', ' + self.staff_id


def test_inheritance():
"""Inheritance."""

# There’s nothing special about instantiation of derived classes: DerivedClassName() creates a
# new instance of the class. Method references are resolved as follows: the corresponding class
# attribute is searched, descending down the chain of base classes if necessary, and the method
# reference is valid if this yields a function object.
person = Person('Bisrat')
employee = Employee('John', 'A23')

assert person.get_name() == 'Bisrat'
assert employee.get_name() == 'John'
assert employee.get_full_id() == 'John, A23'

# Python has two built-in functions that work with inheritance:
#
# - Use isinstance() to check an instance’s type: isinstance(obj, int) will be True only if
# obj.__class__ is int or some class derived from int.
#
# - Use issubclass() to check class inheritance: issubclass(bool, int) is True since bool is
# a subclass of int. However, issubclass(float, int) is False since float is not a subclass
# of int.

assert isinstance(employee, Employee)
assert not isinstance(person, Employee)

assert isinstance(person, Person)
assert isinstance(employee, Person)

assert issubclass(Employee, Person)
assert not issubclass(Person, Employee)


多重继承 Multiple Inheritance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def test_multiple_inheritance():
"""Multiple Inheritance"""
class Clock:
"""Clock class"""

time = '10:17 PM'

def get_time(self):
"""Get current time
Method is hardcoded just for multiple inheritance illustration.
"""
return self.time


class Calendar:
"""Calendar class"""

date = '12/08/2018'

def get_date(self):
"""Get current date
Method is hardcoded just for multiple inheritance illustration.
"""
return self.date

# Python supports a form of multiple inheritance as well. A class definition with multiple
# base classes looks like this.
class CalendarClock(Clock, Calendar):

calendar_clock = CalendarClock()

assert calendar_clock.get_date() == '12/08/2018'
assert calendar_clock.get_time() == '11:23 PM'