[Tutor] list question

Ataulla S H ataulla at gmail.com
Tue Aug 11 15:05:33 CEST 2009


if u want to add new attributes into ur list
u need to import list object in ur class

class customlist(list):
     x = 'your val'
c = customlist()
>>> dir(c)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delslice__', '__dict__', '__doc__', '__eq__', '__ge__',
'__getattribute__', '__getitem__', '__getslice__', '__gt__', '__hash__',
'__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__module__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__',
'__setitem__', '__setslice__', '__str__', '__weakref__', 'append', 'count',
'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort', *'x'*]
>>> c.x
'your val'

On Tue, Aug 11, 2009 at 6:25 PM, Wayne <srilyk at gmail.com> wrote:

>
>
> On Tue, Aug 11, 2009 at 7:17 AM, <proportional at msn.com> wrote:
>
>>  hi i am currently doing the 'livewires' python tutorial lesson 5. i am
>> making a little game that makes a few squares chase a circle around a little
>> grid. i can get the 1 square acting on 1 variable to come up, but the
>> tutorial now wants me to create a list of variables that act under the same
>> class.
>>
>> this is what i have to make the 1 square appear (which works)(i have also
>> cut a lot of the program out to make this email smaller.)
>>
>> class Robot:
>>     pass
>>
>> def place_robot():
>>     global robot
>>     robot = Robot()
>>     robot.x=random_between(0,63)
>>     robot.y=random_between(0,47)
>>     draw_robot()
>>     print robot.x
>>
>> and i cant figure out what to write to make the variable 'robot' work as a
>> list, and then follow all the instructions 3 times. this is what i tried to
>> put in.
>>
>> class Robot:
>>     pass
>>
>> def place_robot():
>>     global robot
>>     r1 = Robot()
>>     r2 = Robot()
>>     r3 = Robot()
>>     robot = [r1,r2,r3]
>>     robot.x=random_between(0,63)
>>     robot.y=random_between(0,47)
>>     draw_robot()
>>     print robot.x
>>
>> i was under the assumption that the instruction
>> robot.x=random_between(0,63) would return a value 3 times for r1 r2 and r3,
>> but instead i get AttributeError: 'list' object has no attribute 'x'. so i
>> got no idea heh. i hope this makes sense, as im really new to programming.
>>
>
> I think what you want is:
>
> for r in robot:
>     r.x = random_between(0,63)
>     r.y = random_between(0,47)
>     draw_robot()
>     print r.x
>
> Now for the why:
> robot is a list of objects - you declared it such with robot = [r1, r2, r3]
> (incidentally you could just do robots = [Robot(), Robot(), Robot()] ), and
> lists don't have x y attributes, which you're trying to access. I presume
> you're really trying to access the x y attributes of your Robot() class,
> which is an entirely different object from your list.
>
> HTH,
> Wayne
>
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Ataulla SH

web:www.kring.com
personal blog:www.ataulla.objectis.net
KRING Technologies India Pvt. Ltd.
1st Floor, Tower B, Infinity Towers, DLF II, Gurgaon-122 002
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090811/d7ed42d8/attachment.htm>


More information about the Tutor mailing list