由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Python Q: function pass in struct pointer, come back with data filled
相关主题
go也是三种paradigm混合的语言也谈OOP跟FP之争
对 (im)mutability 的误解和深度理解OOP里面的Object其实是actor
这么说吧,fp不是否定变量,而是控制变量的范围函数式语言是不是特别费系统资源?
FP更接近人的思维面向数据的编程与面向对象的编程
没人觉得python的string是immutable不爽吗?FP的死穴还是性能
python 区别于其他语言的根本?one more interview question
最近学了一下 Go[合集] C++ template question
从今天开始起,学C++!c++ question
相关话题的讨论汇总
话题: aaa话题: python话题: surprise话题: struct话题: treat
进入Programming版参与讨论
1 (共1页)
w*s
发帖数: 7227
1
in c code i can do this
func(struct aaa *)
{
aaa->f1 = 1;
aaa->f2 = "abc";
}
how to do this in python pls ?
d**o
发帖数: 864
2
create a class aaa
if you give an instance of aaa as a parameter to a function, the reference
is passed.
You don't need to do anything special.
class aaa:
def __init__(self):
self.a=1
def treat(aaa):
aaa.a=2
ins=aaa()
print ins.a
treat(ins)
print ins.a

【在 w*s 的大作中提到】
: in c code i can do this
: func(struct aaa *)
: {
: aaa->f1 = 1;
: aaa->f2 = "abc";
: }
: how to do this in python pls ?

w*s
发帖数: 7227
3
good to know.
any limitations that c++ can do, but python cannot,
for structure, passing buffer, passing by reference ?
thx !

【在 d**o 的大作中提到】
: create a class aaa
: if you give an instance of aaa as a parameter to a function, the reference
: is passed.
: You don't need to do anything special.
: class aaa:
: def __init__(self):
: self.a=1
: def treat(aaa):
: aaa.a=2
: ins=aaa()

s*s
发帖数: 100
4
agreed. Just remember do not reassign the passed parameter.
def treat_surprise(p_a):
b=aaa()
b.a=3
p_a=b # surprise for a C++ programmer!

treat_surprise(ins)

【在 d**o 的大作中提到】
: create a class aaa
: if you give an instance of aaa as a parameter to a function, the reference
: is passed.
: You don't need to do anything special.
: class aaa:
: def __init__(self):
: self.a=1
: def treat(aaa):
: aaa.a=2
: ins=aaa()

f*******n
发帖数: 12623
5
Why is it a surprise? It's the same in C++:
void treat_surprise(aaa *p_a) {
aaa *b = new aaa;
b->a = 3;
p_a = b;
}

【在 s*s 的大作中提到】
: agreed. Just remember do not reassign the passed parameter.
: def treat_surprise(p_a):
: b=aaa()
: b.a=3
: p_a=b # surprise for a C++ programmer!
:
: treat_surprise(ins)

d**o
发帖数: 864
6
If you want to copy an object, normally do a copy explicitly.
Cite from Stackoverflow:
"passing by value" and "passing by reference" are not concepts that are
relevant in Python. The relevant concepts are instead "mutable object" and "
immutable object".

【在 w*s 的大作中提到】
: good to know.
: any limitations that c++ can do, but python cannot,
: for structure, passing buffer, passing by reference ?
: thx !

s*s
发帖数: 100
7
I am thinking this:
void treat_surprise(aaa & p_a) {
.......
.......
}
If you pass by reference, p_a=b will call the copy constructor.

【在 f*******n 的大作中提到】
: Why is it a surprise? It's the same in C++:
: void treat_surprise(aaa *p_a) {
: aaa *b = new aaa;
: b->a = 3;
: p_a = b;
: }

p***o
发帖数: 1252
8
Reference in languages like java and python means pointer in C/C++.

【在 s*s 的大作中提到】
: I am thinking this:
: void treat_surprise(aaa & p_a) {
: .......
: .......
: }
: If you pass by reference, p_a=b will call the copy constructor.

1 (共1页)
进入Programming版参与讨论
相关主题
c++ question没人觉得python的string是immutable不爽吗?
这个function pointer最后的那个int是什么意思?python 区别于其他语言的根本?
怎么这里这么多人学python最近学了一下 Go
问个c++ struct和指针问题从今天开始起,学C++!
go也是三种paradigm混合的语言也谈OOP跟FP之争
对 (im)mutability 的误解和深度理解OOP里面的Object其实是actor
这么说吧,fp不是否定变量,而是控制变量的范围函数式语言是不是特别费系统资源?
FP更接近人的思维面向数据的编程与面向对象的编程
相关话题的讨论汇总
话题: aaa话题: python话题: surprise话题: struct话题: treat