Re: How to validate the __init__ parameters

2010-01-11 Thread Jean-Michel Pichavant
Aahz wrote: In article mailman.2244.1261418090.2873.python-l...@python.org, Jean-Michel Pichavant jeanmic...@sequans.com wrote: class A: def __init__(self, foo = None, bar = None): if len(foo) 5: raise ValueError('foo cannot exceed 5 characters') Bad Idea

Re: How to validate the __init__ parameters

2010-01-10 Thread Aahz
In article mailman.2244.1261418090.2873.python-l...@python.org, Jean-Michel Pichavant jeanmic...@sequans.com wrote: class A: def __init__(self, foo = None, bar = None): if len(foo) 5: raise ValueError('foo cannot exceed 5 characters') Bad Idea -- what happens when

Re: How to validate the __init__ parameters

2010-01-04 Thread Albert van der Horst
In article mailman.2271.1261450134.2873.python-l...@python.org, Steve Holden st...@holdenweb.com wrote: SNIP What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control

Re: How to validate the __init__ parameters

2010-01-04 Thread Chris Rebert
On Mon, Jan 4, 2010 at 10:17 AM, Albert van der Horst alb...@spenarnc.xs4all.nl wrote: snip This triggers a question: I can see the traceback, but it would be much more valuable, if I could see the arguments passed to the functions. Is there a tool? print(locals()) #this actually gives the

Re: How to validate the __init__ parameters

2010-01-04 Thread Phlip
Steve Holden st...@holdenweb.com wrote: What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is

Re: How to validate the __init__ parameters

2010-01-04 Thread Gabriel Genellina
En Mon, 04 Jan 2010 15:17:04 -0300, Albert van der Horst alb...@spenarnc.xs4all.nl escribió: This triggers a question: I can see the traceback, but it would be much more valuable, if I could see the arguments passed to the functions. Is there a tool? Yes, the cgitb module [1]. Despite its

Re: How to validate the __init__ parameters

2010-01-04 Thread Albert van der Horst
In article mailman.423.1262627230.28905.python-l...@python.org, Chris Rebert c...@rebertia.com wrote: On Mon, Jan 4, 2010 at 10:17 AM, Albert van der Horst alb...@spenarnc.xs4all.nl wrote: snip This triggers a question: I can see the traceback, but it would be much more valuable, if I could see

Re: How to validate the __init__ parameters

2009-12-22 Thread Bruno Desthuilliers
Denis Doria a écrit : Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: If you use a property, you'll have the validation in the initializer AND everywhere else too. If you care about

Re: How to validate the __init__ parameters

2009-12-22 Thread Bruno Desthuilliers
Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong* when the wrong type is passed?

Re: How to validate the __init__ parameters

2009-12-22 Thread r0g
Bruno Desthuilliers wrote: Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let things go wrong*

Re: How to validate the __init__ parameters

2009-12-22 Thread Lie Ryan
On 12/22/2009 8:52 PM, Bruno Desthuilliers wrote: Steve Holden a écrit : (snip) What's the exact reason for requiring that a creator argument be of a specific type? So operations on the instances don't go wrong? Well, why not just admit that we don't have control over everything, and just *let

How to validate the __init__ parameters

2009-12-21 Thread Denis Doria
Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct self.bar = bar All examples that I

Re: How to validate the __init__ parameters

2009-12-21 Thread Stephen Hansen
On Mon, Dec 21, 2009 at 9:41 AM, Denis Doria denisdo...@gmail.com wrote: All examples that I saw with property didn't show a way to do it in the __init__. Just to clarify, I don't want to check if the parameter is an int, or something like that, I want to know if the parameter do not use more

Re: How to validate the __init__ parameters

2009-12-21 Thread Jean-Michel Pichavant
Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct self.bar = bar

Re: How to validate the __init__ parameters

2009-12-21 Thread Alf P. Steinbach
* Denis Doria: I thought in something like: class A: def __init__(self, foo = None, bar = None): set_foo(foo) self._bar = bar def set_foo(self, foo): if len(foo) 5: raise something _foo = foo foo = property(setter = set_foo) But looks

Re: How to validate the __init__ parameters

2009-12-21 Thread r0g
Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct self.bar

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Mon, 21 Dec 2009 09:41:22 -0800, Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. There is no best way, since it depends on personal taste. Of course I can use property to check it, but I really want to do it inside the __init__: If you really

Re: How to validate the __init__ parameters

2009-12-21 Thread Lie Ryan
On 12/22/2009 4:41 AM, Denis Doria wrote: Hi; I'm checking the best way to validate attributes inside a class. Of course I can use property to check it, but I really want to do it inside the __init__: class A: def __init__(self, foo, bar): self.foo = foo #check if foo is correct

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: I use assertions myself e.g. foo = 123456 assert len(foo) = 5 Traceback (most recent call last): File stdin, line 1, in module AssertionError Dunno if this would be considered good or bad programming practice by those more experienced

Re: How to validate the __init__ parameters

2009-12-21 Thread r0g
Steven D'Aprano wrote: On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: I use assertions myself e.g. foo = 123456 assert len(foo) = 5 Traceback (most recent call last): File stdin, line 1, in module AssertionError Dunno if this would be considered good or bad programming practice by

Re: How to validate the __init__ parameters

2009-12-21 Thread Steve Holden
r0g wrote: Steven D'Aprano wrote: On Mon, 21 Dec 2009 21:49:11 +, r0g wrote: I use assertions myself e.g. foo = 123456 assert len(foo) = 5 Traceback (most recent call last): File stdin, line 1, in module AssertionError Dunno if this would be considered good or bad programming

Re: How to validate the __init__ parameters

2009-12-21 Thread Steven D'Aprano
On Tue, 22 Dec 2009 00:18:21 +, r0g wrote: Yikes, glad to be set me straight on that one! Thanks :) It's a pity though, I really like the way it reads. Is there anything similar with ISN'T disabled when optimizations are turned on? Yes: an explicit test-and-raise. if not condition: