Newbie References Question
Guy Rabiller
grabiller at 3dvf.net
Thu Sep 26 01:05:05 EDT 2002
Yes you are totaly right.
I'm walking toward this way anyway, but I was just confuse about this
'reference thing' ( wich was my mystake as I forgot that my 'p' was a nested
list of coordinate ).
Thanks for the example Frank.
--
guy rabiller
3d animator / td
grabiller at 3dvf.net
http://grabiller.3dvf.net
Frank Sonnenburg wrote:
> "Guy Rabiller" <grabiller at 3dvf.net> schrieb im Newsbeitrag
> news:3d91c72f$0$8558$79c14f64 at nan-newsreader-02.noos.net...
>> Hi,
>>
>> let say I have:
>> i1 = 1
>> i2 = 2
>> and
>> p = [i1,i2]
>>
>> How can I have:
>> p=[*i1,*i2]
>> rather than
>> p=[**i1,**i2] as it is curently ?
>>
>> ( Sorry for this nasty hybrid syntax )
>>
>> Textualy, how if I want that p[0] refere to the 'i1' container, and
>> not to the refererence it contains ?
>>
>> What I want is that if now I set:
>> i1 = 4
>> that automaticaly:
>> p -> [4,2]
>> and not keeping [1,2]
>
> My proposal: THINK OO!
>
> In my opinion, this is an excellent example to use the full power of
> object oriented programming. Of course, this is a lot of overhead and
> maybe you just wanted a very short approach. But since you told, you
> are a newbie, maybe you are not quite used to this style of
> programming. Just try out and experiment a little.
>
> ===============================================================
>
> # Module point.py
>
>
> class Point:
> def __init__(self, x=0, y=0):
> self._x = x
> self._y = y
>
> def __repr__(self):
> return 'Point (%d,%d)' % (self._x, self._y)
>
> def short_repr(self):
> return '(%d,%d)' % (self._x, self._y)
>
> def set(self, x, y):
> self._x = x
> self._y = y
>
> class Figure:
> def __init__(self, *points):
> self._points = []
> for p in points:
> if not isinstance(p, Point):
> raise TypeError
> self._points.append(p)
>
> def __repr__(self):
> s = 'Figure'
> # Derived class instance?
> classname = self.__class__.__name__
> if classname != 'Figure':
> s = s + ' (%s)' % classname
> if self._points:
> s = s + ', def. by'
> for p in self._points:
> s = s + ' '+p.short_repr()
> else:
> s = s + ', no points defined'
>
> return s
>
>
> #
> # Now it's your turn ;-)
> #
>
> # def set, get, remove, ......
> # ...
>
>
> class Quad(Figure):
> def __init__(self, p0=Point(0,0), p1=Point(0,1),
> p2=Point(1,0), p3=Point(1,1)):
> Figure.__init__(self, p0, p1, p2, p3)
>
> def set_point(self, index, point):
> if type(index) != type(0) or index < 0 or index > 3:
> raise ValueError
> if not isinstance(point, Point):
> raise TypeError
>
> self._points[index] = point
>
> ===============================================================
>
>>>> from point import *
>>>> q = Quad()
>>>> q
> Figure (Quad), def. by (0,0) (0,1) (1,0) (1,1)
>>>> q.set_point(1,Point(555,888))
>>>> q
> Figure (Quad), def. by (0,0) (555,888) (1,0) (1,1)
>
>>>> p = Point(1,2)
>>>> p.set(4,2)
>>>> p
> Point (4,2)
>
> ===============================================================
>
>
> Frank
More information about the Python-list
mailing list