Форум сайта python.su
2
знакомлюсь с ООП
по заданию нужно добавить метод resize
вылетает ошибка в чем проблема, не могу понять…
вот полный код если вдруг нужно
http://pastebin.com/ZpZbdwLj
Офлайн
857
>>> class A: ... ... x = 1 ... ... @property ... def f(self): ... return self.x ... ... # @f.setter ... # def f(self, value): ... # self.x = value ... ... @f.deleter ... def f(self): ... self.x = None ... >>> a = A() >>> a.f = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: can't set attribute >>>
Отредактировано py.user.next (Июль 17, 2014 03:45:41)
Офлайн
2
могли бы вы пожалуйста дать не только ответ, но и прояснить что к чему? так как я хочу понять
Офлайн
2
и я совсем не понял как сделать то что вы написали…
Офлайн
2
разобрался, забыл ‘__’ поставить
self.__width
получается хотел присвоить что то методу self.width, а не переменной
Отредактировано simple_user (Июль 17, 2014 11:36:14)
Офлайн
857
simple_user
но и прояснить что к чему? так как я хочу понять
simple_user
разобрался, забыл ‘__’ поставить
simple_user
и я совсем не понял как сделать то что вы написали
@property def width(self): return self.__width @width.setter def width(self, value): self.__width = value
>>> class A: ... def __init__(self): ... self.__x = 1 ... ... @property ... def x(self): ... return self.__x ... ... @x.setter ... def x(self, value): ... self.__x = value ... ... @x.deleter ... def x(self): ... self.__x = None ... >>> a = A() >>> a.x 1 >>> a.x = 2 >>> a.x 2 >>> del a.x >>> a.x >>> >>>
Отредактировано py.user.next (Июль 17, 2014 13:11:53)
Офлайн
2
теперь я немного улавливаю суть декораторов, пространство имен, благодарю
но
self.__x = value
Офлайн
857
simple_user
ведь вы же сделали так же само по сути, только я не использовал декоратор
>>> class A: ... def m(self): ... pass ... ... def _m(self): ... pass ... ... def __m(self): ... pass ... >>> a = A() >>> a.m() >>> a._m() >>> a.__m() Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute '__m' >>> >>> help(a) >>>
Help on A in module __main__ object:
class A(builtins.object)
| Methods defined here:
|
| m(self)
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
>>> class A: ... __x = 1 ... def m(self): ... self.__x = 2 ... def p(self): ... return self.__x ... >>> a = A() >>> a.m() >>> a.p() 2 >>> a.__x Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'A' object has no attribute '__x' >>>
In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):
_single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.
single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.
Tkinter.Toplevel(master, class_='ClassName')
__double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
__double_leading_and_trailing_underscore__: "magic" objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.
Отредактировано py.user.next (Июль 17, 2014 22:31:12)
Офлайн
2
благодарю, но это я уже понял
я все использовал внутри класа
вот тот метод, уже работающий
найди 10 отличий)
как и говорил просто забыл добавить двойное подчеркивание
def resize(self,width=None,height=None): if (width==None) and (height==None): return False colors=set() if width < self.width: for j in range(width,self.width): for i in range(self.height): colors.add(self[j,i]) if height < self.height: for j in range(width,self.height): for i in range(self.width): colors.add(self[i,j]) self.__colors = self.__colors - colors self.__width = width if width != None else self.__width self.__heiht = height if height != None else self.__height return True
Офлайн
857
simple_user
я все использовал внутри класа
simple_userself.__width = width if width != None else self.__width
self.__width = width if width is not None else self.width
Отредактировано py.user.next (Июль 18, 2014 10:15:51)
Офлайн