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 に答える