为了账号安全,请及时绑定邮箱和手机立即绑定
  • 服务端:

    import socket

    server = socket.socket() # 1. 新建socket
    server.bind(('127.0.0.1', 8999)) # 2. 绑定IP和端口(其中127.0.0.1为本机回环IP)
    server.listen(5) # 3. 监听连接
    s, addr = server.accept() # 4. 接受连接
    print('connect addr:{}'.format(addr))
    while True:
       content =s.recv(1024)
       if len(content) == 0:
           break
       s.send(content)
       print(str(content, encoding='utf-8')) # 接受来自客户端的消息,并打印出来

    s.close()

    客户端:

    import socket

    client = socket.socket() # 1. 新建socket
    client.connect(('127.0.0.1', 8999)) # 2. 连接服务端(注意,IP和端口要和服务端一致)
    while True:
       content = input('>>> ')
       client.send(bytes(content, 'utf-8')) # 发送内容,注意发送的是字节字符串。
       content = client.recv(1024)
       print(str(content, encoding='utf-8'))

    client.close()

    查看全部
  • 导入math模块的方式有两种:

    一种是直接使用 import math ,这样就可以使用 math.函数名 的形式调用math模块中的函数。例如,要计算30度的正弦值,你可以写:

    import math
    x = math.sin(math.radians(30)) # math.radians()是将角度转换为弧度的函数print(x) # 输出0.5

    复制

    另一种是使用 from math import * ,这样就可以直接使用函数名而不用加上 math. 的前缀。例如,要计算60度的余弦值,你可以写:

    from math import *
    x = cos(radians(60)) # radians()是将角度转换为弧度的函数print(x) # 输出0.5

    查看全部
    0 采集 收起 来源:Python导入模块

    2023-07-09

  • # common.py

    def say_hello(name):

        print("Hello", name)

    查看全部
    0 采集 收起 来源:Python定义模块

    2023-07-09

  • class Fib(object):

        def __init__(self):

            self.a = 0

            self.b = 1


        def __call__(self, n):

            result = []

            for i in range(n):

                result.append(self.a)

                self.a, self.b = self.b, self.a + self.b

            return result

    查看全部
  • class Fib:

        def __init__(self, n):

            self.n = n

            self.a = 0

            self.b = 1

            self.index = 0


        def __iter__(self):

            return self


        def __next__(self):

            if self.index < self.n:

                self.a, self.b = self.b, self.a + self.b

                self.index += 1

                return self.a

            else:

                raise StopIteration


        def __len__(self):

            return self.n


        def __repr__(self):

            return "Fib({})".format(self.n)


        def __str__(self):

            return ", ".join(str(x) for x in self)


    # 测试代码

    f = Fib(10)

    print(f) # 打印数列的前10个元素

    print(len(f)) # 打印数列的个数

    查看全部
  • 前两个数的值分别为 0 、1 或者 1、1; 从第 3 个数字开始,它的值是前两个数字的和; 为了纪念他,人们将满足以上两个特征的数列称为斐波那契数列。

    查看全部
  • # 定义Person类

    class Person:

        def __init__(self, name, age):

            self.name = name

            self.age = age


    # 定义SkillMixin类

    class SkillMixin:

        def show_skills(self):

            print(f"{self.name}的技能有:{self.skills}")


    # 定义BasketballMixin类

    class BasketballMixin(SkillMixin):

        def __init__(self):

            self.skills = ["打篮球"]


    # 定义FootballMixin类

    class FootballMixin(SkillMixin):

        def __init__(self):

            self.skills = ["踢足球"]


    # 定义Student类

    class Student(Person):

        def __init__(self, name, age, grade):

            super().__init__(name, age)

            self.grade = grade


    # 定义Teacher类

    class Teacher(Person):

        def __init__(self, name, age, subject):

            super().__init__(name, age)

            self.subject = subject


    # 定义会打篮球的学生类

    class BasketballStudent(Student, BasketballMixin):

        def __init__(self, name, age, grade):

            Student.__init__(self, name, age, grade)

            BasketballMixin.__init__(self)


    # 定义会踢足球的老师类

    class FootballTeacher(Teacher, FootballMixin):

        def __init__(self, name, age, subject):

            Teacher.__init__(self, name, age, subject)

            FootballMixin.__init__(self)


    # 测试代码

    bs = BasketballStudent("小明", 18, "高三")

    bs.show_skills()

    ft = FootballTeacher("李老师", 35, "数学")

    ft.show_skills()

    查看全部
    0 采集 收起 来源:Python中的多态

    2023-07-06

  • 向函数传递任意关键字参数的方法**kw

    for k,v in kw.items():

    查看全部
  • class Student()定义的时候,需要在括号内写明继承的类Person

    在__init__()方法,需要调用super(Student, self).__init__(name, gender),来初始化从父类继承过来的属性

    查看全部
    0 采集 收起 来源:Python继承类

    2023-07-05

  • class Person(object):
       def __init__(self, name, gender):
           self.name = name
           self.gender = gender

    class Student(Person):
       def __init__(self, name, gender, score):
           super(Student, self).__init__(name, gender)
           self.score = score

    class Teacher(Person):
       def __init__(self, name, gender, course):
           super(Teacher, self).__init__(name, gender)
           self.course = course

    p = Person('Tim', 'Male')
    s = Student('Bob', 'Male', 88)
    t = Teacher('Alice', 'Female', 'English')
    # 判断t是否是Person类型
    if isinstance(t, Person):
       print("t is a Person")
    else:
       print("t is not a Person")

    # 判断t是否是Student类型
    if isinstance(t, Student):
       print("t is a Student")
    else:
       print("t is not a Student")

    # 判断t是否是Teacher类型
    if isinstance(t, Teacher):
       print("t is a Teacher")
    else:
       print("t is not a Teacher")

    # 判断t是否是object类型
    if isinstance(t, object):
       print("t is an object")
    else:
       print("t is not an object")

    查看全部
    0 采集 收起 来源:Python判断类型

    2023-07-05

  • 类属性和实例属性同名,实力属性优先;

    不能在外部更改类属性;

    __代表类属性私有化,外部无法访问;

    1.带有两个下划线开头的函数是声明该属性为私有,不能在类地外部被使用或直接访问。
    2.init函数(方法)支持带参数的类的初始化 ,也可为声明该类的属性
    3.init函数(方法)的第一个参数必须是 self(self为习惯用法,也可以用别的名字),后续参数则可 以自由指定,和定义函数没有任何区别。

    查看全部
  • class Animal(object):
       __localtion = 'Asia'
       def __init__(self, name, age):
           self.name = name
           self.age = age

       @classmethod
       def set_localtion(cls, localtion):
           cls.__localtion = localtion

       @classmethod
       def get_localtion(cls):
           return cls.__localtion

    print(Animal.get_localtion()) # ==> Asia
    Animal.set_localtion('Afica')
    print(Animal.get_localtion()) # ==> Africa


    使用了类属性和类方法的概念。

    类属性是定义在类中且在所有实例之间共享的变量,类方法是定义在类中且可以直接通过类名调用的函数。

    私有属性是以两个下划线开头的变量,它们只能在类的内部访问,不能在外部访问。

    为了让外部能够获取私有属性的值,我们可以定义一个类方法,它可以通过cls参数访问到类属性,并返回它的值。

    我们就可以通过Counter.get_count()来获取__count的值了。

    查看全部
  • class Animal:

        def __init__(self, age, name, location):

            self.__age = age

            self.__name = name

            self.__location = location

        def set_age(self, new_age):

            if isinstance(new_age, int) and new_age > 0:

                self.__age = new_age

            else:

                print("Invalid age")

        def get_age(self):

            return self.__age

        def set_name(self, new_name):

            if isinstance(new_name, str) and new_name:

                self.__name = new_name

            else:

                print("Invalid name")

        def get_name(self):

            return self.__name

        def set_location(self, new_location):

            if isinstance(new_location, str) and new_location:

                self.__location = new_location

            else:

                print("Invalid location")

        def get_location(self):

            return self.__location

    a = Animal(3, "Tom", "New York")

    print(a.get_age())

    a.set_age(5)

    print(a.get_age())

    print(a.get_name())

    a.set_name("Jerry")

    print(a.get_name())

    print(a.get_location())

    a.set_location("London")

    print(a.get_location())

    查看全部
  • # 定义一个Animal类
    class Animal:
       # 初始化方法,接收age, name, location参数
       def __init__(self, age, name, location):
           # 把参数赋值给私有属性,用双下划线开头表示
           self.__age = age
           self.__name = name
           self.__location = location

       # 定义一个方法,用来修改私有属性__age的值
       def set_age(self, new_age):
           # 判断新的年龄是否合法,如果是正整数,则修改,否则提示错误
           if isinstance(new_age, int) and new_age > 0:
               self.__age = new_age
           else:
               print("Invalid age")

       # 定义一个方法,用来获取私有属性__age的值
       def get_age(self):
           # 返回私有属性__age的值
           return self.__age

       # 定义一个方法,用来修改私有属性__name的值
       def set_name(self, new_name):
           # 判断新的名字是否合法,如果是非空字符串,则修改,否则提示错误
           if isinstance(new_name, str) and new_name:
               self.__name = new_name
           else:
               print("Invalid name")

       # 定义一个方法,用来获取私有属性__name的值
       def get_name(self):
           # 返回私有属性__name的值
           return self.__name

       # 定义一个方法,用来修改私有属性__location的值
       def set_location(self, new_location):
           # 判断新的位置是否合法,如果是非空字符串,则修改,否则提示错误
           if isinstance(new_location, str) and new_location:
               self.__location = new_location
           else:
               print("Invalid location")

       # 定义一个方法,用来获取私有属性__location的值
       def get_location(self):
           # 返回私有属性__location的值
           return self.__location

    # 创建一个Animal对象,传入年龄,名字和位置参数
    a = Animal(3, "Tom", "New York")
    # 调用get_age方法,打印对象的年龄
    print(a.get_age())
    # 调用set_age方法,修改对象的年龄为5
    a.set_age(5)
    # 再次调用get_age方法,打印对象的年龄
    print(a.get_age())
    # 调用get_name方法,打印对象的名字
    print(a.get_name())
    # 调用set_name方法,修改对象的名字为"Jerry"
    a.set_name("Jerry")
    # 再次调用get_name方法,打印对象的名字
    print(a.get_name())
    # 调用get_location方法,打印对象的位置
    print(a.get_location())
    # 调用set_location方法,修改对象的位置为"London"
    a.set_location("London")
    # 再次调用get_location方法,打印对象的位置
    print(a.get_location())

    查看全部
  • def __init__()

    下划线是两个字符_连续?

    查看全部

举报

0/150
提交
取消
课程须知
本课程是Python入门的后续课程 1、掌握Python编程的基础知识 2、掌握Python函数的编写 3、对面向对象编程有所了解更佳
老师告诉你能学到什么?
1、什么是函数式编程 2、Python的函数式编程特点 3、Python的模块 4、Python面向对象编程 5、Python强大的定制类

微信扫码,参与3人拼团

意见反馈 帮助中心 APP下载
官方微信
友情提示:

您好,此课程属于迁移课程,您已购买该课程,无需重复购买,感谢您对慕课网的支持!