OneCoder Avatar
OneCodercoderli.com · 1023 篇博文
🐍 Python

Python 与大数据

🎨 视觉封面

Python练习-模块和初级面像对象

📅 2022-08-01·✍️ onecode·计算中...·⏱️ 3 分钟

本部分练习Python模块和初级面像对象相关知识。练习代码如下:

Python74 行
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
' a test module '

__author__ = 'OneCoder Lihz'

import sys


def test():
    args = sys.argv
    print("args:", args)
    if len(args) == 1:
        print('Hello, world!')
    elif len(args) == 2:
        print('Hello, %s!' % args[1])
    else:
        print('Too many arguments!')


# 测试用法,当import导入时,__name__不等于main,代码不会执行。
if __name__ == '__main__':
    test()

# __doc__访问模块注释
print(__doc__)

print(sys.path)


# 类
class Student:
    # 必须的属性
    def __init__(self, name, score):
        # 类访问限制,私有属性
        self.__name = name
        self.__score = score

    def print_score(self):
        print(self.__name, "'s score is:", self.__score)


stu1 = Student("Sun One", 98)
# 随意添加属性
stu1.age = 18
stu1.print_score()


class Fruit:
    def __init__(self, name):
        self.__name = name

    def print_name(self):
        print("Fruit name is", self.__name)


class Apple(Fruit):
    def print_name(self):
        print("This is an apple")


class Pear(Fruit):
    pass
    # def __init__(self, name):
    #     self.__name = name
    #     print("Print name in class pear", self.__name)


fruit = Fruit("Grape")
fruit.print_name()
apple = Apple("Apple")
apple.print_name()
pear = Pear("Pear")
pear.print_name()
看完题解去实战!OneCoder C++ 信奥远征闯关平台 (oi.coderli.com)
💡 OneCoder 资源指引

所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI

🤝 技术交流与答疑

欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。

📚

猜你想读 · 相关文章推荐

OneCoder

OneCoder

一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com

读者讨论与留言

共 0 条讨论
邮箱必填 · 支持 Markdown 格式与直接回复
💬 还没有读者留言,快来成为第一个讨论者吧!