将一个指针数组传递给python类

时间:2012-11-07 23:25:24

标签: python class pointers for-loop python-2.7

我是python的新手。我熟悉C ++。我会将流动的C ++代码转换为python。

class School{
    School(char *name, int id, Student* pointers){
    {
        this.name=name;
        this.weapon=weapon;
        this.students=pointers;
    }
    print(){
        for(int i=0;i<10;i++){
            this.students.print();
        }
    }
};

如您所见,我正在尝试将指针传递给Student类型的对象数组 我不确定python是否允许我传递指针。这就是我在python中所做的

class School():
    def __init__(self, name, *students):
        self.name=name
        self.students=students

    def display(self):
        for student in self.students
            student.display()

3 个答案:

答案 0 :(得分:0)

在python中,完全可以找到将整个列表传递给构造函数 无论如何,Python都会将列表作为参考传递。

class School():
    def __init__(self, name, students):
        self.name=name
        self.students=students

    def display(self):
        for student in self.students
            student.display()

在这种情况下,self.students是对原始students列表

的引用

测试这个的好方法是以下代码:

original = ['a','b']

class my_awesome_class:
    def __init__(self, a):
        self.my_list = a

    def print_list(self):
        self.my_list.append("my_awesome_class")
        print(self.my_list)

my_class = my_awesome_class(original)

print (original)
my_class.print_list()
print (original)

如需额外阅读,您可能需要查看python variable names from a c perspective

答案 1 :(得分:0)

Python没有指针。或者说,Python中的所有是一个指针,包括名称,列表中的条目,属性...... Python是一种“传递引用”语言。

以下是一些简单的例子:

In [1]: a = ['hello', tuple()]  # create a new list, containing references to
                                # a new string and a new tuple. The name a is
                                # now a reference to that list.

In [2]: x = a  # the name x is a reference to the same list as a.
               # Not a copy, as it would be in a pass-by-value language

In [3]: a.append(4)  # append the int 4 to the list referenced by a

In [4]: print x
['hello', (), 4]  # x references the same object

In [5]: def f1(seq):  # accept a reference to a sequence
   ...:     return seq.pop()  # this has a side effect:
                              # an element of the argument is removed.

In [6]: print f1(a)  # this removes and returns the last element of
4                    # the list that a references

In [7]: print x  # x has changed, because it is a reference to the same object
['hello', ()]

In [8]: print id(a), id(x)
4433798856 4433798856  # the same

In [9]: x is a  # are x and a references to the same object?
Out[9]: True

Python提供了高级构造,用于在C中执行需要指针运算的操作。因此,您永远不必担心给定变量是否为指针,就像您永远不必担心内存管理一样。

答案 2 :(得分:0)

你输入的内容基本上就是你想要的,但有一个关键的区别。请注意学生之前没有明星:

class School():
    def __init__(self, name, students):
        self.name=name

        self.students=students

    def display(self):
        for student in self.students:
            student.display()

这意味着你将实例化一个这样的学校(为学生组成一个构造函数):

s1 = Student('Bob')
s2 = Student('Fill')

school = School("Generic School",[s1,s2])

如果__init__方法看起来像def __init__(self, name, *students):,那么您可以使用以下方法实例化完全相同的学校

s1 = Student('Bob')
s2 = Student('Fill')

school = School("Generic School",s1,s2)

原因是*students中的__init__(并且这适用于任何方法)意味着“获取其余传递的非关键字参数并将其粘贴在{{1}中} list。