🐍 Python
Python 与大数据
🎨 视觉封面Python练习-模块和初级面像对象
本部分练习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 资源指引
所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI
🤝 技术交流与答疑
欢迎加入:C++ GESP/CSP 考级答疑群(688906745) 与 Java/Python交流群(982860385),点击可直接加群。
📚
猜你想读 · 相关文章推荐
Python 与数据工程 · 技术实战⏱️ 5 分钟
python练习-Hello World
OneCoder 技术详解:python练习-Hello World,涵盖 Python技术研究、Python 核心原理、代码实战与易错点复盘。
阅读全文 →
Python 与数据工程 · 技术实战⏱️ 9 分钟
python练习-高级特性
OneCoder 技术详解:python练习-高级特性,涵盖 Python技术研究、Python 核心原理、代码实战与易错点复盘。
阅读全文 →
Python 与数据工程 · 技术实战⏱️ 8 分钟
python练习-函数式编程(一)
本部分练习Python函数式编程。练习高阶函数、map、reduce等常用函数。
阅读全文 →
OneCoder
一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com
读者讨论与留言
💬 还没有读者留言,快来成为第一个讨论者吧!

