Jan-09-2018, 05:59 PM
The following code works, as long as I use primarily public instance variables. When I change my public self.l to its private form self._l, the code breaks
I get a traceback error:
Also: can anyone concisely explain the purpose behind @property decorators in general and @classmethod? From what I understand of them, both seem to basically offer another way to instantiate objects. I haven't used any examples of @classmethod, but the only use I see for them is to let me run something like @fooclass.deleter to delete instances on a case by case basis.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
class Emp(): def __init__( self , f, l): self .f = f self .l = l self .e = self .f + self .l + '@gmail.com' @property def fullname( self ): return '{} {}' . format ( self .f, self .l) @fullname .setter def fullname( self , name): f,l = name.split( ' ' ) self .f = f self .l = l @fullname .deleter def fullname( self ): print ( 'deleted' ) self .f = None self .l = None emp1 = Emp( 'Foo' , 'Bar' ) emp2 = Emp( 'Moo' , 'Soo' ) emp2.l = 'foo' print (emp2.f) print (emp1.l) print (emp1.fullname) del emp2.fullname |
Quote:Traceback (most recent call last):
File "decs1.py", line 30, in <module>
print(emp1.l)
AttributeError: 'Emp' object has no attribute 'l'
Also: can anyone concisely explain the purpose behind @property decorators in general and @classmethod? From what I understand of them, both seem to basically offer another way to instantiate objects. I haven't used any examples of @classmethod, but the only use I see for them is to let me run something like @fooclass.deleter to delete instances on a case by case basis.