1. Python变量到底是什么?

  Python和Java中的变量本质不一样,python的变量实质是一个指针 int str,便利贴

  a = 1

  # 1. a贴在1上面

  # 2. 它的过程是先生成对象,然后贴便利贴。

  # 3. is 是指的标签贴是否一样。

  a = 1

  b = 1

  这个是一样,用的是小整数的内部inter机制的内部优化。

  == 用的是__eq__这个魔法函数。

  # 4. 常用的用法是isinstance或者type() is,这两种是通用的。type实际上是指向了这个对象的。

2. del语句和垃圾回收的关系:

  py中的垃圾回收采用的是引用计数。

# a =
# b = a
# del a # 引用计数器减去1,等于0的时候py会回收。 a = object()
b = a
del a
print(b) # b可以打印,a打印不出来了
print(a) # C:\Python37\python.exe F:/QUANT/练习/chapter01/type_object_class.py
# Traceback (most recent call last):
# File "F:/QUANT/练习/chapter01/type_object_class.py", line , in <module>
# print(a)
# NameError: name 'a' is not defined
# <object object at 0x000002909F4AAA70>
#
# Process finished with exit code class A:
del __del__(self):
pass

  记住:对应的魔法函数是__del__

3. 默认空的list的可变,一个景点的参数传递问题。

def add(a,b):
a += b
return a
class Company:
def __init__(self,name,staffs=[]):
self.name = name
self.staffs = staffs def add(self,staff_name):
self.staffs.append(staff_name) def remove(self,staff_name):
self.staffs.remove(staff_name) if __name__ == '__main__':
# a =
# b =
# c = add(a,b)
# print(c)
# print(a,b)
#
# # a = [,]
# b = [,]
# c = add(a,b)
# print(c)
# print(a,b)
# [, , , ]
# [, , , ][, ] # a = (,)
# b = (,)
# c = add(a,b)
# print(c)
# print(a,b)
# (, , , )
# (, ) (, ) com1 = Company("con1",["bobby1","bobby2"])
com1.add("bobby3")
com1.remove("bobby1")
print(com1.staffs)
# ['bobby2', 'bobby3'] com2 = Company("com2")
com2.add("bobby")
print(com2.staffs)
# ['bobby'] com3 = Company("com3")
com3.add("bobby5")
print(com2.staffs,com3.staffs)
# ['bobby', 'bobby5']['bobby', 'bobby5'] print(com2.staffs is com3.staffs)
# True # 这个原因是运用了一个可变对象=[]
print(Company.__init__.__defaults__)

  记住:在类中可变对象的话容易造成错误的,把a就行修改掉了。

  记住:其实这里就是引用参数的问题。引用参数是用可变对象来实现的。

Python说文解字_杂谈08的更多相关文章

  1. Python说文解字_杂谈05

    1. isinstance和type: is和==符号,is指的是内存地址,是不是一个对象,ID知否相同 集成链 class A: pass class B(A): pass b = B() prin ...

  2. Python说文解字_杂谈09

    1. 元类编程代码分析: import numbers class Field: pass class IntField(Field): # 数据描述符: # 初始化 def __init__(sel ...

  3. Python说文解字_杂谈07

    1. 深入dict from collections.abc import Mapping,MutableMapping # dict 属于mapping类型 a = {} print(isinsta ...

  4. Python说文解字_杂谈01

    1. Python在Ubuntu下面下载Python 2. 安装依赖包 sudo apt-get update sudo apt-get install build-essential python- ...

  5. Python说文解字_杂谈06

    1. 序列类型的分类: 容器类型:list.tuple,deque 扁平序列:str.bytes.bytearray.array.array 可变序列:list.dequte.bytearray.ar ...

  6. Python说文解字_杂谈04

    1. 鸭子类型: 当你看到一只鸟走来像鸭子,游泳起来像鸭子,叫起来也像鸭子,他么他就可以叫做鸭子.任何可迭代的对象.一样的方法,可以用可迭代的话,就可以迭代的组合打印.__getitem__可以塞到任 ...

  7. Python说文解字_杂谈03

    1. 我们从前面的知识得到,所有的类都要继承自object这个基类(超类),另外我们知道“继承”可以继承类的属性和方法.我们起始通过type创建类的时候,自然而然的也会从ojbect继承他的一些属性和 ...

  8. Python说文解字_杂谈02

    1. Py中三个中啊哟的概念type.object和class的关系. type生成了int生成了1 type->class->obj type用来生成类对象的 object是最顶层的基类 ...

  9. Python说文解字_详解元类

    1.深入理解一切接对象: 1.1 什么是类和对象? 首先明白元类之前要明白什么叫做类.类是面向对象object oriented programming的重要概念.在面向对象中类和对象是最基本的两个概 ...

随机推荐

  1. a标签的超链接提交form表单

    <form action="/home/search" method="get" id="search_form"><di ...

  2. cf749 D. Leaving Auction

    #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL long long #define N 200005 #de ...

  3. 哈希(hash)理解

    转载自https://www.cnblogs.com/mingaixin/p/4318837.html 一.什么是哈希?(一种更复杂的映射) Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就 ...

  4. 【Android】家庭记账本手机版开发报告三

    一.说在前面 昨天 对第一天的框架结构进行了四方面的完善 今天 对界面显示和逻辑结构进行完善 问题 无 二.界面展示完善 1.使用可回收的列表recyclerView展示账单的信息,并设置数据项为卡片 ...

  5. UVA - 1612 Guess (猜名次)(贪心)

    题意:有n(n<=16384)位选手参加编程比赛.比赛有3道题目,每个选手的每道题目都有一个评测之前的预得分(这个分数和选手提交程序的时间相关,提交得越早,预得分越大).接下来是系统测试.如果某 ...

  6. React + umi +antd+antv/g6 实现力图

    官方示例效果:http://antv.alipay.com/zh-cn/g6/2.x/demo/net/2017-link-data.html 改编效果: 实现步骤: 环境:nodejs.yarn/n ...

  7. RN命令的使用

    RN中文网站 https://reactnative.cn/docs/getting-started/ 创建项目 1.最新版本项目react-native init MyApp 使用可行版本  rea ...

  8. Short Essay你真的会写了吗?

    提到short essay(可能其他essay也一样),很多同学都很头疼.“没有思路?不知从何下笔?没有亮点?”等等,这些都是同学们的致命伤,因此,short essay就成为了广大留学生的“送命题” ...

  9. The 2019 China Collegiate Pro gramming Contest Harbin Site (F. Fixing Banners)

    F. Fixing Banners time limit per test 1 second memory limit per test 512 megabytes input standard in ...

  10. GTK入门

    环境准备 官网下载 GTK 源码包,因为本机 GLib 版本不够,下载一个非最新版的 GTK3.8.0 先学习用 直接阅读 "/gtk+-3.8.0/docs/reference/gtk/h ...