类似于: 随机导入 class Gazelle(对象): def __init __(self): self.pos = 0,0 #创建实例列表 gazelles = [Gazelle ()for x in range(5)] #更新瞪羚位置 deltaxmin,deltaxmax = -100,+ 100 deltaymin,deltaymax = -100,+ 100 $ g $ b for g gazelles: g.pos =(g.pos [0] + random.randint(deltaxmin,deltaxmax) ), g.pos [1] + random.randint(deltaymin,deltaymax)) 以上是未经测试的。 - Paddy。 实际上,Python _only_支持指针:它们是对象的名称。所以 例如,如果我写x = Gazelle(...),那么我创建名称x 指向Gazelle的一个实例。实例的存储是由Python管理的魔法。如果我当时要说y = x,我也会有 名称y这指向同一个实例。 同样值得注意的是,包括整数,字符串和列表在内的所有东西都是 对象。因此,指向对象的指针是python中唯一的存储 类。因此,名称可以指向任何类型的任何对象。 结果,一个数组在Python中,它通常是一个列表,只是一个列表 的指针。他们可以指向字符串,整数,其他列表或任何东西。并且 因为它们存储指针,它们实际上可以包含它们自己! 要演示: 1 a [1,2,3] 1 a [1,2,3] [1,'''',[1,2,3],[...]] Python'聪明到不打印出来循环引用。 最后,值得指出的是,在这样的语言中,哪里有 没有任意指针(就像有的一样) C),指向对象的指针称为 引用。我只是使用了指针因为你做了;)。 Howdy, a (possibly) quick question for anyone willing to listen.I have a question regarding lists and Classes; I have a class called"gazelle" with several attributes (color, position, etc.) and I needto create a herd of them. I want to simulate motion of individualgazelles, but I don''t want to have to go through and manually updatethe position for every gazelle (there could be upwards of 50). I wasplanning to create an array of these gazelle classes, and I was goingto iterate through it to adjust the position of each gazelle. That''show I''d do it in C, anyway. However, Python doesn''t support pointersand I''m not quite sure how to go about this. Any help you can providewould be greatly appreciated.Thanks a lot! -Ryan 解决方案 As I understand it, every name in Python is a pointer. class Gazelle(object):def __init__(self):self.x = 0 g_list =[]for x in range(10):g_list.append(Gazelle()) for g in g_list:g.x = 10 print g_list[2].x Something like: import randomclass Gazelle(object):def __init__(self):self.pos = 0, 0 # create a list of instancesgazelles= [ Gazelle() for x in range(5)] # update gazelle positionsdeltaxmin, deltaxmax = -100, +100deltaymin, deltaymax = -100, +100for g in gazelles:g.pos = (g.pos[0] + random.randint(deltaxmin, deltaxmax),g.pos[1] + random.randint(deltaymin, deltaymax) ) The above is untested by the way. - Paddy. Actually, Python _only_ supports pointers: they''re the names of objects. Sofor example, if I write x = Gazelle(...), then I create the name "x" thatpoints to an instance of Gazelle. The storage for the instance ismanaged ''magically'' by Python. If I were then to say "y = x", I''d also havea name "y" that points to the same instance. It''s also worth noting that everything, including ints, strings and lists areobjects as well. Because of this, a pointer to an object is the only storageclass in python. Therefore, a name can point to any object of any type. As a result, an "array" in Python, which is commonly a list, is simply a listof pointers. They can point to strings, ints, other lists or anything. Andbecause they store pointers, they can actually include themself! To demonstrate: 1a[1, 2, 3] 1a[1, 2, 3][1, ''a'', [1, 2, 3], [...]]Python''s clever enough to not print out the circular reference. Finally, it''s worth pointing out that in a language like this, where there areno arbitrary pointers (as there are in C), the pointer-to-object is called areference. I just used "pointer" because you did ;). 这篇关于对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-28 18:11