1

我是 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()
4

3 回答 3

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)

原因是*studentsin __init__(这适用于任何方法)意味着“获取其余传递的非关键字参数并将它们粘贴到student列表中。

于 2012-11-08T02:45:12.233 回答
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)

如需额外阅读,您可能需要从 ac 角度查看python 变量名称

于 2012-11-07T23:41:07.253 回答
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 中进行指针运算的事情。因此,您无需担心给定变量是否为指针,就像您无需担心内存管理一样。

于 2012-11-08T00:01:17.303 回答