Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Newbie

Reply
Thread Tools

Python Newbie

 
 
Ethan Furman
Guest
Posts: n/a
 
      02-24-2013
On 02/24/2013 03:38 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
>>> intX = 32 # decl + init int var

>> How is it not obvious that "intX" is an integer *without* the comment?

>
> Indeed the assignment is enough to deduce "intX" is an int. The comment is there to let me know it is unlikely intX appears earlier in the code. Please, let me do things my way until I find reasons to the contrary.


Of course you can, but wouldn't you rather find reasons to the contrary by us telling you, instead of tripping
over something yourself?

For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
X as an integer:

--> intX = 32
--> intX = intX / 3.0
--> intX
10.6666666666

--
~Ethan~
 
Reply With Quote
 
 
 
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-25-2013
> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>
> X as an integer:
>
> --> intX = 32
>
> --> intX = intX / 3.0
>
> --> intX
>
> 10.6666666666
>


Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python.

Peter
 
Reply With Quote
 
 
 
 
piterrr.dolinski@gmail.com
Guest
Posts: n/a
 
      02-25-2013
> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>
> X as an integer:
>
> --> intX = 32
>
> --> intX = intX / 3.0
>
> --> intX
>
> 10.6666666666
>


Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python.

Peter
 
Reply With Quote
 
Joshua Landau
Guest
Posts: n/a
 
      02-25-2013
On 25 February 2013 00:08, <(E-Mail Removed)> wrote:

> > For example (I believe it's already been mentioned) "declaring" intX

> with some integer value does *nothing* to maintain
> >
> > X as an integer:
> >
> > --> intX = 32
> >
> > --> intX = intX / 3.0
> >
> > --> intX
> >
> > 10.6666666666
> >

>
> Yes I did see that it is possible to redefine the type of a variable. But
> I don't think I would ever do this intentionally; need to be really careful
> with Python.



Not necessarily.

Python duck types. If you don't know what that means, Google's got a ton on
it.

Take a look at my really bad quadratic equation solver. It supports integer
input, float input and complex input. It will output a list of two floats
or complex numbers.

That's a use for having one variable have different types. You'll find
thousands of parallels in real, working code.

Hence, you don't really need to be careful. You'd probably benefit if you
stopped thinking of supporting type-changing as "dangerous" and started
thinking of it as "useful".

 
Reply With Quote
 
Ethan Furman
Guest
Posts: n/a
 
      02-25-2013
On 02/24/2013 04:08 PM, (E-Mail Removed) wrote:
>> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>>
>> X as an integer:
>>
>> --> intX = 32
>>
>> --> intX = intX / 3.0
>>
>> --> intX
>>
>> 10.6666666666
>>

>
> Yes I did see that it is possible to redefine the type of a variable.


And that right there is one of the key aspects of Python: there are no variables, only objects, and objects' types
cannot be changed. objects can be labeled with names, but the names are more like sticky notes, and can be peeled off
and stuck on some other object.

--
~Ethan~
 
Reply With Quote
 
Mark Lawrence
Guest
Posts: n/a
 
      02-25-2013
On 25/02/2013 00:08, (E-Mail Removed) wrote:
>> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>>
>> X as an integer:
>>
>> --> intX = 32
>>
>> --> intX = intX / 3.0
>>
>> --> intX
>>
>> 10.6666666666
>>

>
> Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python.
>
> Peter
>


Yes this is a big downside with Python. Sadly it means us poor Python
programmers have to waste a lot of time and effort testing our code,
unlike those who use statically typed languages which work perfectly
once they've been compiled.

--
Cheers.

Mark Lawrence

 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      02-25-2013
In article <(E-Mail Removed)>,
Ethan Furman <(E-Mail Removed)> wrote:

> On 02/24/2013 03:38 PM, (E-Mail Removed) wrote:
> >
> >>> intX = 32 # decl + init int var
> >> How is it not obvious that "intX" is an integer *without* the comment?

> >
> > Indeed the assignment is enough to deduce "intX" is an int. The comment is
> > there to let me know it is unlikely intX appears earlier in the code.
> > Please, let me do things my way until I find reasons to the contrary.

>
> Of course you can, but wouldn't you rather find reasons to the contrary by us
> telling you, instead of tripping
> over something yourself?
>
> For example (I believe it's already been mentioned) "declaring" intX with
> some integer value does *nothing* to maintain
> X as an integer:
>
> --> intX = 32
> --> intX = intX / 3.0
> --> intX
> 10.6666666666


I could imagine a getattr-based implementation of DBC (Design By
Contract) which does use the variable name to enforce type. Unclear if
this is a Good Thing, a Bad Thing, or a just plain Crazy Thing. In any
cae, it would be a neat (if somewhat advanced) exercise for somebody
interested in enforcing types and looking to explore some of the more
arcane corners of Python.

class DBC_Example:
# Ad-libbing this, code not tested
def __setattr__(self, name, value):
if name.startswith('int'):
assert isinstance(value, int)
self.__dict__[name] = value
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2013
On Sun, 24 Feb 2013 16:08:01 -0500, Roy Smith wrote:

> In article <(E-Mail Removed)>,
> Chris Angelico <(E-Mail Removed)> wrote:
>
>> > no need to remember what's an object and what's not -- everything is
>> > an object

>
> Well, not quite everything. If I write:
>
> if foo:
> do_this()
> and_this()
>
> the code block making up the body of the "if" statement is not an
> object. In some languages, it is.



In Python, that code block isn't any *thing*. It's merely a small part of
the enclosing code block, which *is* an object.

When we say "everything is an object" in Python, we're talking about
values, not arbitrary language constructs. The "*3" bit of "y = x*3" is
not a value, a for-loop is not a value, and the delay you experience when
you call time.sleep(30) is not a value, so none of these things are
objects. This is not to reduce the importance of these things as
programming concepts, but they aren't the kind of things we mean when we
say everything is an object.


--
Steven
 
Reply With Quote
 
Oscar Benjamin
Guest
Posts: n/a
 
      02-25-2013
On 25 February 2013 00:08, <(E-Mail Removed)> wrote:
Chris Angelico wrote:
>> For example (I believe it's already been mentioned) "declaring" intX with some integer value does *nothing* to maintain
>>
>> X as an integer:
>>
>> --> intX = 32
>>
>> --> intX = intX / 3.0
>>
>> --> intX
>>
>> 10.6666666666
>>

>
> Yes I did see that it is possible to redefine the type of a variable. But I don't think I would ever do this intentionally; need to be really careful with Python.


You do need to be careful around types in Python (as in all
languages). It took some time for me to understand how to use types in
Python. After a while I came to realise that not knowing exactly the
type of a particular object is not as much of a problem as I initially
thought. Once you understand how to use this ambiguity to your
advantage it becomes possible to write very flexible code that can be
reused without ambiguity in situations that you haven't yet
anticipated.

The key mental hurdle, I think, is to realise that instead of relying
on compilation errors to spot (a small subset of) your programming
errors, you are relying on runtime exceptions. Python still gives
errors when you use an object in a way that is inconsistent with its
type; you just don't see those errors at compile time.

The trickier cases are ones where two types are very similar and can
be used similarly in most, but not all, situations. An example of this
would be the one that Chris has highlighted where an object that you
expected to be an int is actually a float. I find that I need to be
careful when using division on quantities that I expected to be
integers (true in all languages) and careful about the notation used
in a numeric literal. Once you get used to it, you will find it easy
to see that the '.0' that Chris appended was deliberate in order to
control the type of the resulting object.


Oscar
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      02-25-2013
In article <(E-Mail Removed)>,
(E-Mail Removed) wrote:

> Yes I did see that it is possible to redefine the type of a variable. But I
> don't think I would ever do this intentionally


One does not need language features to protect themselves against things
they do intentionally. They need language features to protect
themselves against things they do by accident. Different languages
protect you from different things.

Compare, for example, C++ and Python.

C++ protects you against accidentally passing an int where you were
supposed to pass a float. Well, no, with automatic type promotion,
that's a bad example. But it does prevent you from passing an IntThing
where you were supposed to pass a FloatThing (assuming IntThing is not a
subclass of FloatThing, and a few other details).

But, Python protects you from dereferencing a null pointer, or
double-freeing a pointer. There's just no way to even write those
concepts in Python.

You pays your money and you takes your chances. Pick which type of
protection you feel is more important and use the language which gives
you that.

> need to be really careful with Python.


You need to be really careful with all programming languages. You just
need to be careful about different things.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib, winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 12 04-22-2005 12:19 PM
Re: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Jaime Wyant Python 0 04-21-2005 12:49 PM
RE: (Python newbie) Using XP-SP2/MSVC6: No Python24_d.lib,winzip barfs on Python-2.4.1.tar, cannot download bzip2 Bill Davy Python 0 04-19-2005 09:27 AM
Python Newbie. Python on PPC, please assist. pythonnewbie Python 5 10-12-2004 05:51 PM
Creating an OLE server document in Python (MFC/OLE/COM/Python newbie) Drew Pihera Python 0 02-04-2004 07:48 PM



Advertisments