Property with parameter...

Marc Jeurissen marc.jeurissen at ua.ac.be
Mon Sep 13 09:00:40 EDT 2004


Why not just like this or does this create 'hidden traps'?


class A:
     def __init__(self):
         self.__name = {}

     def __setname(self, name):
         for tag in name:
             self.__name[tag] = name[tag]

     def __getname(self):
         return self.__name

     name = property(__getname, __setname)

     def show(self):
         print self.__name



a = A()
a.name['one'] = 1
a.name['two'] = 2
a.show()
-> {'two': 2, 'one': 1}

print a.name['two']
-> 2


Marc



kepes.krisztian wrote:

> Hi !
> 
> I want to create a property that can use parameter(s).
> In Delphi I can create same thing (exm: Canvas.Pixel[x,y] -> 
> Canvas.GetPixel(self,X,Y):integer; 
> Canvas.SetPixel(self,X,Y,Color::integer);
> 
> class A(object):
>      def __init__(self):
>          self.__Tags={}
>      def GetTag(self,tname):
>          return self.__Tags.get(tname,None)
>      def SetTag(self,tname,value):
>          self.__Tags[tname]=Value
>      Tag=property(GetTag,SetTag)
> 
> a=A()
> print a.Tag('A')
> print a.Tag['A']
> 
> But it is seems to be not possible in this way.
> 
> How to be can ?
> 
> Thanx for help !
> KK
> 
> 




More information about the Python-list mailing list