Byte-of-python笔记代码3:Object.py

本文介绍了Python中类的创建及使用方法,包括初始化方法、类变量与对象变量的区别,并演示了异常处理的基本用法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#-*-coding:utf-8-*-

###创建Person类
# class Person:
#     pass
#
#
# p=Person();#创建Person类的一个对象
# print(p)

# ###方法
# class Person:
#     def SayHello(self):
#         print("Hello My Lord !")
# d=Person();
# d.SayHello()
#
# Person().SayHello()


# ###:__init__,类在实例化时,会被首先执行__init__中的东西,亦可称为:初始化方法
#
# class Person:
#     def __init__(self,name):
#         self.name=name;
#     def SayHello(self):
#         print("__init__方法初始化的name:{0}".format(self.name))
# Person("xiaoxie").SayHello()


# ###  类变量,和对象变量
# class Robot:
#     population=0
#     def __init__(self,name):
#         self.name=name
#         print("正在创造机器人,名字为:{0}".format(name))
#         Robot.population+=1;
#
#     def die(self):
#         print("{0}挂了!".format(self.name))
#         Robot.population-=1;
#
#         if Robot.population==0:
#             print("{0}是最后的一个机器人".format(self.name))
#         else:
#             print("仍然还有{0}个机器人".format(Robot.population))
#
#     def sayHello(self):
#         print("MyLord,我是机器人:{0},请指教".format(self.name))
#
#     def sayCount(self):
#         print("报告大人,现在共有机器人:{0}台".format(Robot.population))
#
# b1=Robot("number1")
# b1.sayHello()
# b1.sayCount()
#
# b2=Robot("number2")
# b2.sayCount()
# b2.sayHello()
#
# b1.die()
# b2.die()

# ###输入、输出
# def reverse(text):
#     return text[::-1]
#
# def is_palindrome(text):
#     return text==reverse(text)
#
# someing=input("请输入文本:")
#
# if(is_palindrome(someing)):
#     print("文本为回文!")
# else:
#     print("文本不是回文!")

# ###文件操作
# import os
# text="这是python创建的文件\nhahahha"
# f=open('tt.txt','w')
# f.write(text)
# f.close()
#
# print(os.path.abspath('tt.txt'))
#
# f=open('tt.txt')
# while True:
#     line=f.readline()
#     if len(line)==0:
#         break
#     else:
#         print(line,end='')
# f.close()

###Unicode
# import io
#
# txt=io.open("tt.txt","wt",encoding='utf-8')
# txt.write("哈哈哈,滴滴滴,大大大,嘿嘿嘿,qwert67")
# txt.close()
#
# read=io.open("tt.txt",encoding="utf-8").read()
# print(read)


###异常try:......except Exception:......
# try:
#     s=input("input Something ")
# except EOFError:
#     print("格式有错")
# else:
#     print("没有错误操作")

# ##自定义异常类
# class shortInputException(Exception):
#     def __init__(self,length,atleast):
#         Exception.__init__(self)
#         self.length=length
#         self.atleast=atleast
#
# try:
#     s=input("input something:")
#     if(len(s)<3):
#         raise shortInputException(len(s),3)
# except shortInputException as ex:
#     print("输入长度为{0},最少输入长度要求为{1}".format(ex.length,ex.atleast))
# else:
#     print("输入长度为:{0}".format(len(s)))


####   try......finally

# import sys,time
#
# f=None
# try:
#     f=open("tt.txt",encoding="utf-8")
#     while True:
#         line=f.readline()
#         if len(line)==0:
#             break
#         print(line)
#         sys.stdout.flush()#为了让打印的字符立马显示。
#         time.sleep(2)
# except IOError:
#     print("找不到文件!")
# except KeyboardInterrupt:
#     print("中断执行(通常是输入^C)")
# finally:
#     if f:
#         f.close()
#     print("close f")

# with open("tt.txt",encoding="utf-8") as f:
#     for line in f:
#         print(line)







这里给出下一篇的链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

象话

打赏犹如太阳穴的枪口

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值