首页 > 其他分享 >Day3

Day3

时间:2024-04-14 18:22:52浏览次数:22  
标签:__ self Day3 instance ex print class

# Exceptions # try: #     num = int(input("input a number: ")) # except ValueError as ex: #     print(ex) # else: #     print("continue") # note: the type of ex is ValueError
# handling different type of exceptions # there are some other types of exceptions like ZeroDivisionError, ValueError, etc. # how to control different errors in one try blcok # try: #     file1 = open("day3.py") #     age = int(input("Age: ")) #     tmp = 10/age #     file1.close() # except (ValueError, ZeroDivisionError) as ex: #     print(ex) # else: #     print("continue") # use finally clause to execute the last operation # this piece of code above will be executed at the end of try block
# use "with" statement to make the code more readable and cleaner # the with statement below is to automatically release external res # try: #     with open("day3.py") as file1, open("day2.py") as file2: #         print("file open") # except NameError as ex: #     print(ex)

# Class # class attributes and instance attributes # class attributes belong to all the instance of the class, they differ from instance attributes in that they are # owned by one specific instance of the class and are not share between instances
# class methods and instance methods # here method, draw, above is an instance method, we need an instance to call it, by contrast, we do not need an instance to call
# class Point: #     default_color = "red"
#     def __init__(self, x, y): #         self.x = x #         self.y = y
#     @classmethod #     def zero(cls): #         return cls(0, 0)
#     def draw(self): #         print(f"({self.x},{self.y})")

# point = Point(1, 2) # point = Point.zero() # point.draw()

# Magic methods # magic methods usually used for overloading the operators # new words: under score '_' class Point:     def __init__(self, x, y):         self.x = x         self.y = y
    def __str__(self):         return f"{self.x},{self.y}"
    def __eq__(self, other):         return self.x == other.x and self.y == other.y

point = Point(1, 2) print(str(point))
point1 = Point(1, 2) print(point == point1) # here we use "str()" to call magic method

标签:__,self,Day3,instance,ex,print,class
From: https://www.cnblogs.com/meetalone/p/18134487

相关文章

  • Day37代码随想录(1刷) 动态规划
    509.斐波那契数斐波那契数 (通常用 F(n) 表示)形成的序列称为 斐波那契数列 。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:F(0)=0,F(1) =1F(n)=F(n-1)+F(n-2),其中n>1给定 n ,请计算 F(n) 。示例1:输入:n=2输出:1解......
  • Day31 线程安全一
    Day31线程安全一一、概念线程安全是指在多线程环境下,对共享数据的操作不会导致数据出现不一致或不确定的情况,保证多个线程同时访问共享资源时不会产生竞态条件(RaceCondition)或其他并发问题。重要性:确保线程安全是编写并发程序时必顫考虑的重要问题二、实现方法加锁......
  • Day34代码随想录(1刷)贪心
    435.无重叠区间给定一个区间的集合 intervals ,其中 intervals[i]=[starti,endi] 。返回 需要移除区间的最小数量,使剩余区间互不重叠 。示例1:输入:intervals=[[1,2],[2,3],[3,4],[1,3]]输出:1解释:移除[1,3]后,剩下的区间没有重叠。示例2:输入:in......
  • 算法打卡day37|动态规划篇05| Leetcode1049.最后一块石头的重量II、494.目标和、474.
    算法题Leetcode1049.最后一块石头的重量II题目链接:1049.最后一块石头的重量II 大佬视频讲解:最后一块石头的重量II视频讲解 个人思路和昨天的分割等和子集有些相像,这道题也是尽量让石头分成重量相同的两堆,相撞之后剩下的石头最小,这样就化解成01背包问题了。解法......
  • 学习Java Day3-05 (流程控制-循环结构while,do……while,for,增强for)
    循环结构while循环do…while循环for循环在Java5中引入了一种主要用于数组的增强型for循环。while循环while是最基本的循环,它的结构为:while(布尔表达式){ //循环内容}只要布尔表达式为ture,循环就会一直执行下去。我们大多数情况是会让循环停止下来的,我们需要一......
  • 【JavaWeb】Day32.MySQL概述——数据库设计-DDL(一)
    项目开发流程需求文档:    在我们开发一个项目或者项目当中的某个模块之前,会先会拿到产品经理给我们提供的页面原型及需求文档。设计:    拿到产品原型和需求文档之后,我们首先要做的不是编码,而是要先进行项目的设计,其中就包括概要设计、详细设计、接口设计、......
  • 书生·浦语大模型全链路开源体系——学习笔记day2&day3--纯纯新手入门
    学习链接:tutorial/helloworld/hello_world.mdatmain·InternLM/tutorial(github.com) 【精彩,照着做就能体验很多本来遥不可及的东西】笔记分享链接:https://github.com/InternLM/tutorial/discussions/37 本笔记定位是对学习链接的补充和小白发牢骚,希望大佬能愿意点评一......
  • JAVA语言学习-Day3
    参考教学视频:秦疆Day3面向对象什么是面向对象面向过程:第一步做什么,第二步做什么面向对象:分类的思维模式,分类然后对某个分类下的细节进行面向过程的思索(以类的形式组织代码,以对象的方式组织(封装)数据)static:和类一起加载构造器必须和类的名字相同必须没有返回值类型,也不......
  • PHP代码审计——Day3-Snow Flake
    漏洞解析//实现了一个基本的MVC(Model-View-Controller)结构,通过动态加载控制器类和数据,并调用控制器的方法来实现基本的页面渲染。//自动加载函数,用于动态加载类文件。当使用尚未定义的类时,PHP会自动调用该函数来加载类文件。此处,__autoload函数会尝试加载与类名$className......
  • 稀碎从零算法笔记Day37-LeetCode:所有可能的真二叉树
    今天的每日一题,感觉理解的还不够深,有待加深理解题型:树、分治、递归链接:894.所有可能的真二叉树-力扣(LeetCode)来源:LeetCode题目描述给你一个整数 n ,请你找出所有可能含 n 个节点的 真二叉树 ,并以列表形式返回。答案中每棵树的每个节点都必须符合 Node.val==0 ......