On Dec 3, 9:34 am, mChicago <
jmon.mons...@googlemail.com> wrote:
> Let me rephrase and check that its ok in Python then:
>
> Constructors can't return any value, so if something does go wrong
> then they should instead throw exceptions.
> The problem in C++ / other low level programming is that in the
> constructor, you allocate some memory, throw the exception, the system
> won't call the deconstructor and boom, memory leaking. So as long as
> you clean up *before* throwing exception, then everythings cool. (And
> of course there are further problems in various languages where
> extending a class means that your new class's constructor should also
> throw the same exception and not where some languages try, to catch
> and ignore). I think a lot of people say you shouldn't throw
> exceptions in constructors (and I think thats the advice in Code
> Complete) and to use a initaliser instead because it just means
> minion^W junior programmers who arn't as good programers cant cause
> too many problems.
Python deals with the memory. There are no attempts to save memory by
having "value types", all objects behave basically the same way (there
are a few primitive types, but you never seem to hit the corners of
the model in practice). C++ is a world of pain in comparison. It may
seem very elegant when it's working well, but it's not as elegant as
not having to care.
> But Python seems ok in that regards, because I dont use malloc or
> anything daft I dont need to worry about memory leaking in [in this
> instance].
True. The downside of course is that when you exhaust your n GB of
memory, your program might well die horribly when the interpreter
falls over. But you can just ignore that fact and it will never happen
in practice. The same is probably true of JVMs.
You can get memory leaks in Python. Having said that, I've never had
one that I've noticed in my five to ten years of coding Python, but I
usually write short-lived scripts and web apps.
> On a slightly different aspect:
>
> The main thing that is going to catch me out is the move from a
> strongly typed language to a weakly typed language and method
> declarations: i.e. when I declare a method that has an integer being
> passed, I am very used to knowing that its always an integer being
> passed, but in Python it could be anything - so when writing methods.
It catches you out less than you'd think.
def square(n):
return n * n
You could pass in a string, but it would fail pretty quickly. On the
other hand, wow, it works for complex numbers too, and I did no extra
work...
> Is it usual to check the value types are the right thing (i.e. like int
> (val)) or is it usually a case of just write it and it will throw its
> own exceptions and trust in the person calling your method having read
> your docstring where you said it was an int to figure it out? Are
> there quick shortcuts to checking input vars? [obviously there are
> cases where you would definitely want to check if the particular
> function was doing something mission critical]
Just let it be untyped (strictly, polymorphic) until it starts causing
problems. You could scatter type checks around willy nilly, but hardly
any of them would ever be used and they would hide the intention of
your code, make it harder to maintain. Basically, it's very difficult
to get an object of the wrong type any distance into most code before
the code tries to make it do something it can't, which usually causes
either a TypeError or an "AttributeError: 'Cat' object has no
attribute 'quack'".
This can occasionally cause problems, most commonly with string/list
confusion (keeping strings iterable in Python 3 was a bit
contentious), but they are quite rare and the productivity gain makes
it easily worth it. Google "Duck typing" if you're still interested.
In answer to your question, you check a type like this when you have
to::
if not isinstance(x, (int, basestring)):
raise TypeError("Expected int or string, got %r" % type(x))
If you want to know your types inside out, you can often compile your
code to C++ with Shedskin, which will annotate your Python source with
the types used, but treat that as an off-the-wall suggestion rather
than something anyone actually does.
Tom