Re: 'classmethod' object has only read-only attributes

2009-11-25 Thread Peter Otten
> u...@host:~> python ~/tmp/t.py > Traceback (most recent call last): > File "/home/user/tmp/t.py", line 1, in > class Foo(object): > File "/home/user/tmp/t.py", line 5, in Foo > bar.myattr='test' > TypeError: 'classmethod

'classmethod' object has only read-only attributes

2009-11-25 Thread Thomas Guettler
/tmp/t.py", line 1, in class Foo(object): File "/home/user/tmp/t.py", line 5, in Foo bar.myattr='test' TypeError: 'classmethod' object has only read-only attributes (assign to .myattr) -- Thomas Guettler, http://www.thomas-guettler.de/ E-Mail: guettl

Re: Read Only attributes, auto properties and getters and setters

2009-02-12 Thread josh logan
On Feb 12, 12:27 pm, TechieInsights wrote: > Ok... for some closure I have written a class to automate the > process.  It takes getters and setters and deleters and then sets the > property automatically.  Sweet! > > class AutoProperty(type): >         def __new__(cls, name, bases, methoddict): >

Re: Read Only attributes, auto properties and getters and setters

2009-02-12 Thread TechieInsights
Ok... for some closure I have written a class to automate the process. It takes getters and setters and deleters and then sets the property automatically. Sweet! class AutoProperty(type): def __new__(cls, name, bases, methoddict): processed = [] getter =

Re: Read Only attributes, auto properties and getters and setters

2009-02-12 Thread TechieInsights
On Feb 12, 9:27 am, josh logan wrote: > On Feb 12, 10:58 am, TechieInsights wrote: > > > > > Oh... one other thing that would be really cool is to do this with AOP/ > > descriptors!  I just haven't been able to get that to work either. > > Basics... > > > @readonly > > class MyClass(object): > >

Re: Read Only attributes, auto properties and getters and setters

2009-02-12 Thread josh logan
On Feb 12, 10:58 am, TechieInsights wrote: > Oh... one other thing that would be really cool is to do this with AOP/ > descriptors!  I just haven't been able to get that to work either. > Basics... > > @readonly > class MyClass(object): >         def __init__(self, x): >                 self.set_x

Re: Read Only attributes, auto properties and getters and setters

2009-02-12 Thread TechieInsights
Oh... one other thing that would be really cool is to do this with AOP/ descriptors! I just haven't been able to get that to work either. Basics... @readonly class MyClass(object): def __init__(self, x): self.set_x(x) def get_x(self): return self._

Read Only attributes, auto properties and getters and setters

2009-02-12 Thread TechieInsights
Ok, so I noticed some of the modules (such as many of the datetime attributes) are read-only, which means the __setattr__ and __set__ methods are intercepted... pretty easy. I am looking for a way to automate this. The real goal is not to have read only attributes, but to have getters and

Re: best practices for making read-only attributes of an object

2006-03-15 Thread James Stroud
bruno at modulix wrote: > Tim Chase wrote: >> >>class Foo: > > old-style classes are deprecated, please use new-style classes: > class Foo(object): > This should be re-phrased to 'Use new-style classes, or else!' py> class Foo: ... def __init__(self, color): ... self._color = color ...

Re: best practices for making read-only attributes of an object

2006-03-15 Thread bruno at modulix
Tim Chase wrote: > I've set up an object and would like to make certain attributes > read-only (or at least enforce it without doing extra work, as per > name-mangling or the like). Ideally, the property would be set in the > __init__, and then not be allowed to change. > > The best solution I've

best practices for making read-only attributes of an object

2006-03-15 Thread Tim Chase
I've set up an object and would like to make certain attributes read-only (or at least enforce it without doing extra work, as per name-mangling or the like). Ideally, the property would be set in the __init__, and then not be allowed to change. The best solution I've been able to come up wit

Re: read-only attributes

2006-02-10 Thread john peter
Thank you for the suggestion!bruno at modulix <[EMAIL PROTECTED]> wrote: limodou wrote:> On 2/10/06, john peter wrote:(snip)>> what do i have to do if i want my application code to have>>read-only>> attributes?>>> I think you may consider property() built-in

Re: read-only attributes

2006-02-10 Thread bruno at modulix
limodou wrote: > On 2/10/06, john peter <[EMAIL PROTECTED]> wrote: (snip) >> what do i have to do if i want my application code to have >>read-only >> attributes? >> > I think you may consider property() built-in function: > > property( [fget[, fse

Re: read-only attributes

2006-02-09 Thread limodou
On 2/10/06, john peter <[EMAIL PROTECTED]> wrote: > while reading the lib manual, i came across mentions of read-only > attributes. > for example, a method has a read-only attribute (called _im_self ?) for > binding > a class instance to the method. is such a facilit

read-only attributes

2006-02-09 Thread john peter
while reading the lib manual, i came across mentions of read-only attributes. for example, a method has a read-only attribute (called _im_self ?) for binding a class instance to the method. is such a facility available to custom application code? if so, what do i have to do if i want my

Re: Unexpected behavior of read only attributes and super

2005-12-08 Thread Samuel M. Smith
> >> Then why wasn't __class__ added to c.__dict__ ? Looks like namespace >> searching to me. > > No, as you conclude later, __class__ is special, so you can still > assign > to __class__ even when __slots__ is defined because it's not > considered > a normal attribute. But note that __class__

Re: Unexpected behavior of read only attributes and super

2005-12-08 Thread Steven Bethard
gt;> c.__iter__ = 4 > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: 'C' object attribute '__iter__' is read-only Here, Python is trying to set the "__iter__" attribute of the object. Since you defined __slots__,

Re: Unexpected behavior of read only attributes and super

2005-12-08 Thread Samuel M. Smith
methods when slots defined versus when slots is not defined. >>> c.__iter__ = 4 Traceback (most recent call last): File "", line 1, in ? AttributeError: 'C' object attribute '__iter__' is read-only >>> super(C,c).__iter__ = 4 Traceback (most r

Re: Unexpected behavior of read only attributes and super

2005-12-07 Thread Steven Bethard
Samuel M. Smith wrote: > On 06 Dec, 2005, at 20:53, Steven Bethard wrote: >> You can always shadow class-level attributes in the instance dict. >> (That's what you were doing.) If you want to (try to) replace an >> attribute in the class dict, you need to use the class object, not an >> instance o

Re: Unexpected behavior of read only attributes and super

2005-12-07 Thread Samuel M. Smith
On 06 Dec, 2005, at 20:53, Steven Bethard wrote: > Samuel M. Smith wrote: >> The dict class has some read only attributes that generate an >> exception >> if I try to assign a value to them. >> I wanted to trap for this exception in a subclass using super but it >

Re: Unexpected behavior of read only attributes and super

2005-12-06 Thread Steven Bethard
Samuel M. Smith wrote: > The dict class has some read only attributes that generate an exception > if I try to assign a value to them. > I wanted to trap for this exception in a subclass using super but it > doesn't happen. > > class SD(dict): >pass > [

Re: Unexpected behavior of read only attributes and super

2005-12-06 Thread Samuel M.Smith
Even more strangeness If I define the class to use slots class SD(dict): __slots__ = ['a','b'] s = SD() >>> s.__iter__ >>> s.__iter__ = 5 Traceback (most recent call last): File "", line 1, in ? AttributeError: 'SD' object attribute '__iter__' is read-only Then I get the read only

Unexpected behavior of read only attributes and super

2005-12-06 Thread Samuel M. Smith
I have been playing around with a subclass of dict wrt a recipe for setting dict items using attribute syntax. The dict class has some read only attributes that generate an exception if I try to assign a value to them. I wanted to trap for this exception in a subclass using super but it

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Roy Smith
Paul Rubin wrote: > The example I quoted used an assignment expression inside a > lambda. The person who posted it, That was me. > and the person who followed it up > with the setattr alternative, both didn't notice that the assignment > expression wasn't valid Python

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Paul Rubin
Duncan Booth <[EMAIL PROTECTED]> writes: > An assignment expression, if such a thing existed wouldn't help here. > > The point being that the expression must be evaluated inside the exception > handler in assertRaises, so you either need to delay the evaluation with a > lambda, or by passing the

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Duncan Booth
Duncan Booth wrote: > An assignment expression, if such a thing existed wouldn't help here. Although of course it would help if still inside a lambda. -- http://mail.python.org/mailman/listinfo/python-list

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Duncan Booth
Paul Rubin wrote: > Peter Hansen <[EMAIL PROTECTED]> writes: >> > You want something like >> > self.assertRaises(AttributeError, lambda: self.combat.value = 1) >> >> Or, combining the two responses and avoiding the lambda: >> >> self.assertRaises(AttributeError, setattr, self.combat, 'value',

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Paul Moore
Peter Hansen <[EMAIL PROTECTED]> writes: > Roy Smith wrote: >> You want something like >> self.assertRaises(AttributeError, lambda: self.combat.value = 1) > > Or, combining the two responses and avoiding the lambda: > > self.assertRaises(AttributeError, setattr, self.combat, 'value', 1) > > Hmm..

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Paul Rubin
Peter Hansen <[EMAIL PROTECTED]> writes: > > You want something like > > self.assertRaises(AttributeError, lambda: self.combat.value = 1) > > Or, combining the two responses and avoiding the lambda: > > self.assertRaises(AttributeError, setattr, self.combat, 'value', 1) > > Hmm... this might b

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Peter Hansen
Roy Smith wrote: You want something like self.assertRaises(AttributeError, lambda: self.combat.value = 1) Or, combining the two responses and avoiding the lambda: self.assertRaises(AttributeError, setattr, self.combat, 'value', 1) Hmm... this might be a case where the lambda form is actually the m

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Roy Smith
In article <[EMAIL PROTECTED]>, Paul Moore <[EMAIL PROTECTED]> wrote: > I have a class with a read-only attribute, and I want to add a unit > test to ensure that it really *is* read-only. I can do this as > > def test_readonly(self): > """Value and multiplier must be readonly""" >

Re: Unittest - testing properties (read-only attributes)

2005-02-21 Thread Diez B. Roggisch
Paul Moore wrote: > I have a class with a read-only attribute, and I want to add a unit > test to ensure that it really *is* read-only. I can do this as > > def test_readonly(self): > """Value and multiplier must be readonly""" > try: > self.combat.value = 1 > self.fail("Value is not read onl

Unittest - testing properties (read-only attributes)

2005-02-21 Thread Paul Moore
I have a class with a read-only attribute, and I want to add a unit test to ensure that it really *is* read-only. I can do this as def test_readonly(self): """Value and multiplier must be readonly""" try: self.combat.value = 1 self.fail("Value is not rea