Python摸鱼记录-1

大咕咕的博客终于复活了


大咕咕自学Python的小笔记

·存储方法

全 是 对 象

常量只会存一份,表示相同常量的标识符会都指向到一个地址去

·is和==

is比地址,==比内容

a=2
b=2
if a is b:
    print("yes")
else:
    print("no")
a=[0,1]
b=[0,1]
if a is b:
    print("yes")
else:
    print("no")
a=2
b=2
if a == b:
    print("yes")
else:
    print("no")
a=[0,1]
b=[0,1]
if a == b:
    print("yes")
else:
    print("no")
输出
yes
no
yes
yes

 

· import & module

python文件可以直接作为module来import

import p as m1 #将p.py作为m1模块引入

m1.f1() #调用m1中的f1()函数

注意import时会同时解释并执行对应module里的所有代码

如果想要一些代码不执行,可以在它们前面加一行判断语句

if __name__ == '__main__': #仅当模块名与当前正在解释的文件名相同时
    #code here

·切片

s[a:b] #[a,b)
s[a:] #[a,len)
s[a::b] #a,a+b,a+2b...... 隔着切
s[::b] #0,b,2b......
s[::-b] #len-1,len-1-b,len-1-2b...... 倒着切
s[-a:-b] #[len-a,len-b) 

·OOP相关

初始化  

class c(object):
    def __init__(self,str1,str2):
        self.name=str1
        self.ID=str2

 @property和@attr.setter

把方法变为属性

class testc(object):
    def __init__(self,str):
        self.sname=str
        self.sid=str+"0"

    @property
    def name(self):
        return self.sname

    @name.setter
    def setid(self,str):
        self.sid=str+"0"

c1=testc("233")
print(c1.name)
c1.setid="2333"
print(c1.sid)
输出:

233

23330

__slot__

限定对象可绑定的属性,不对子类的对象生效

@staticmethod 静态方法

@classmethod 类方法 第一个参数名约定为cls,代表当前类相关信息的对象

并没有好好学过OOP,不深入了

 

 
posted @ 2020-12-14 09:17  Speranza_Leaf  阅读(156)  评论(0)    收藏  举报