Python参数传入,自己实现mutable string
2018-03-22 15:57
169 查看
参数传入有两种:
1.passing by value
2.passing by reference
C++ can pass-by-value and pass-by-referenceStraightforward. You can simulate pass-by-reference with pointers. eg:void grow(int& age){}it's easier to write a swap method this way.Java is pass-by-valuePrimitive Types (non-object built-in types) are simply passed by value. Passing Object References feels like pass-by-reference, but it isn't. What you are really doing is passing references-to-objects by value.OK, so what about Python?Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.
python中string是immutable的,那么如何实现自己的mutable string 呢?
1.passing by value
2.passing by reference
C++ can pass-by-value and pass-by-referenceStraightforward. You can simulate pass-by-reference with pointers. eg:void grow(int& age){}it's easier to write a swap method this way.Java is pass-by-valuePrimitive Types (non-object built-in types) are simply passed by value. Passing Object References feels like pass-by-reference, but it isn't. What you are really doing is passing references-to-objects by value.OK, so what about Python?Python passes references-to-objects by value (like Java), and everything in Python is an object. This sounds simple, but then you will notice that some data types seem to exhibit pass-by-value characteristics, while others seem to act like pass-by-reference... what's the deal?It is important to understand mutable and immutable objects. Some objects, like strings, tuples, and numbers, are immutable. Altering them inside a function/method will create a new instance and the original instance outside the function/method is not changed. Other objects, like lists and dictionaries are mutable, which means you can change the object in-place. Therefore, altering an object inside a function/method will also change the original object outside.
python中string是immutable的,那么如何实现自己的mutable string 呢?
class MutableString(object): def __init__(self, data): self.data = list(data) def __repr__(self): return "".join(self.data) def __setitem__(self, index, value): self.data[index] = value def __getitem__(self, index): if type(index) == slice: return "".join(self.data[index]) return self.data[index] def __delitem__(self, index): del self.data[index] def __add__(self, other): self.data.extend(list(other)) def __len__(self): return len(self.data)
相关文章推荐
- python 3中使用getattr和*args时, 出现传入参数不一致的问题
- C++嵌入Python程序(二):参数传入及返回
- Python实现日志自动记录功能(一个思路代码,详细可以自己扩展)
- python flask 服务器端实现接口,post,get 方式,参数获取方法
- Python+getopt实现命令行带参数
- 自己实现Struts2(六)实现参数拦截器并调试框架
- 在实现ReportServer的时候,如何判断当前传入的参数值为null
- Python def函数的定义、使用及参数传递实现代码
- Python+Hadoop Streaming实现MapReduce(如何给map和reduce的脚本传递参数)
- Python实现接受任意个数参数的函数
- python脚本程序,传入参数*要用单引号'*'
- python 3中使用getattr和*args时, 出现传入参数不一致的问题
- 元类会自动将你通常传给‘type’的参数作为自己的参数传入
- python *args **kwargs,传入不固定的参数给函数,或者传入很多的内容给函数,常用在构造函数中。
- Python之调用WCF实现复杂参数序列化
- Python自己实现迭代器
- Python实现带参数与不带参数的多重继承示例
- GridView 动态添加模板列并绑定数据 实现从外部直接传入控件 和 绑定字段参数
- python(39):argparse的用法,从外部传入指定参数
- VC 实现传入不定参数个数 字符串输出