Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python Newbie

Reply
Thread Tools

Python Newbie

 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2013
On Sun, 24 Feb 2013 16:08:06 -0800, piterrr.dolinski 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.


Variables do not have types in Python.

Reset your thinking. Python is a dynamic language with name bindings and
strongly-typed objects, not a static language with strongly-typed
variables. If you don't understand the difference, ask. But so long as
you make the wrong assumptions about the language, you will have a bad
time.

You will find programming much easier, and more pleasant, if you learn
the semantics and idioms of the language you are using, instead of trying
to treat every language as the same.


> But I don't think I would ever do this intentionally; need to be really
> careful with Python.


Not at all. The only difference is whether you get a compiler error or a
runtime error. Instead of:

10 Write code.
20 Compile.
30 If compiler error, GO TO 10.
40 REM code compiles, but it still needs to be tested
50 Test code.
60 If error, GO TO 10.
70 Deploy.

we have:

10 Write code.
20 Test code.
30 If error, GO TO 10.
40 Deploy.



--
Steven
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2013
On Sun, 24 Feb 2013 11:40:05 -0800, piterrr.dolinski wrote:

>> > if (some statement): # short form
>> >
>> > rather than
>> >
>> > if (some statement == true): # long form

>>
>>
>> What all those ugly brackets are for?
>>
>>

> Mark,
>
> Back in the day when C was king, or take many newer long established
> languages (C#, Java),


Python is older than either C# or Java. Why have those languages paid no
attention to the innovations of Python, instead of copying the
misfeatures of C?

Pascal and Algol and Fortran are older than C. Why did C introduce
unnecessary brackets when these older languages did not need them?


> the use of () has been widespread and mandated by
> the compilers. I have never heard anyone moan about the requirement to
> use parentheses.


You have not been paying attention.

In many ways, C has been a curse on programming. It has trained large
numbers of coders to expect and *demand* poor syntax.


> Now come Python in which parens are optional, and all
> of a sudden they are considered bad and apparently widely abandoned. Do
> you really not see that code with parens is much more pleasing visually?


That's funny. Perhaps you should be programming in Lisp.


> I could understand someone's reluctance to use parens if they are very
> new to programming and Pythons is their first language. But my
> impression here is that most group contributors are long-time
> programmers and have long used () where they are required. Again, I'm
> really surprised the community as a whole ignores the programming
> "heritage" and dumps the parens in a heartbeat.


(Because they are unnecessary) (visual noise) (that don't add anything)
(useful) (to the reader's understanding) (of the code).



--
Steven
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-25-2013
On Sun, 24 Feb 2013 17:40:54 -0500, Mitya Sirenef wrote:

> But if block doesn't have to be inside a function, right? It needs to be
> inside a module, but then again everything is inside a module, but it
> wouldn't be very object-oriented if the module was the only object in
> Python .


Python doesn't have code blocks as distinct values. I suppose you could
fake it using compile() and eval() by hand, but it wouldn't work very
well.

Ruby-style code blocks have been requested for many years. GvR has given
his support to this *in principle*, but it depends on somebody thinking
up decent, unambiguous syntax that works with the rest of Python.


--
Steven
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      02-25-2013
On Mon, Feb 25, 2013 at 12:04 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> Not at all. The only difference is whether you get a compiler error or a
> runtime error. Instead of:
>
> 10 Write code.
> 20 Compile.
> 30 If compiler error, GO TO 10.
> 40 REM code compiles, but it still needs to be tested
> 50 Test code.
> 60 If error, GO TO 10.
> 70 Deploy.
>
> we have:
>
> 10 Write code.
> 20 Test code.
> 30 If error, GO TO 10.
> 40 Deploy.


The advantage of compile-time errors is that you don't have to wait
for *that branch* to be executed. So they're hugely advantageous when
it comes to the "obvious problems" like syntactic errors... and looky
here, Python does exactly that The only difference between "static"
and "dynamic" is how much is done at each phase.

If your program has no inputs or side effects, the compiler could
theoretically convert it into a single static output statement. Voila!
All your run-time errors have become compile-time errors. Conversely,
you could compile your application from source just before running it.
Voila! Everything's a run-time error, even the omission of a semicolon
in C. Okay, both of those are pretty stupid examples, but there's
really no fundamental difference.

ChrisA
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-25-2013
On 02/24/2013 03:43 PM, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I wanted Python to register what type of variable I'm after. So I
> init my vars accordingly, int might be 0, float 0.0 and string with
> null, err... None.


As several people on the list have pointed out, there are no variables
in Python. Let me repeat that. There are no variables in python. Thus
to say, x=5 does not tell anything about what x can represent in the
future. It merely says that the *name* "x" is bound to the object,
which happens to be an immutable integer object that represents 5. That
5 can never change. Ever. In the future you can assign x to another
object, maybe the result of an expression. So none of what you did
"initialializes" a "variable." Probably sounds like I'm just being
pedantic, but if you can learn to see the wisdom and strengths of
python's way of doing things you'll end up writing very rapid code and
very correct code too.

Python's type system is dynamic but it is, in fact, a strong type
system. You can't just arbitrary convert an object from one type to
another. The aspect of python that gives it so much power over
statically-typed languages is that as long as a type supports the
interface you want to work with, the type just doesn't matter. Nor
should it. This allows tremendous code re-use. For example I can
totally change out one object for another object of a completely
different type and my algorithms and logic still work. It's similar to
C# generics, but much more powerful. In Python, it's called
duck-typing. It's a very powerful concept.
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-25-2013
On 02/24/2013 03:40 PM, Mitya Sirenef wrote:
> But if block doesn't have to be inside a function, right? It needs
> to be inside a module, but then again everything is inside a module, but
> it wouldn't be very object-oriented if the module was the only object in
> Python .


A module indeed fits into the OOP paradigm. It's called a singleton and
I love the fact that I can define and use them in python without
wrapping them in tons of boilerplate class and factory code.
 
Reply With Quote
 
Michael Torrie
Guest
Posts: n/a
 
      02-25-2013
On 02/24/2013 06:04 PM, Steven D'Aprano wrote:
> Variables do not have types in Python.
>
> Reset your thinking. Python is a dynamic language with name bindings and
> strongly-typed objects, not a static language with strongly-typed
> variables. If you don't understand the difference, ask. But so long as
> you make the wrong assumptions about the language, you will have a bad
> time.


Yes, but according to my computer language theory class, strictly
speaking, python has no variables, only names and objects, which for the
most part aren't mutable. A variable by definition is a box in memory
that you can write to. The closest thing python has to that are
instances of mutable types like a list.

"a=5" certainly doesn't allocate a box in memory somewhere and stick 5
in it. And then later "a += 1" or "a=6" doesn't mutate the value stored
in the box represented by a. Instead it allocates a new object and
makes a refer to that new object.

I know all this is what you meant, but with the original poster's
frustrations with python, it's important that he just throw out the
notion of variations entirely because sooner or later that will get him
in trouble here, like if he tries to make an empty list be a default
value for a function parameter.
 
Reply With Quote
 
Dennis Lee Bieber
Guest
Posts: n/a
 
      02-25-2013
On Sun, 24 Feb 2013 10:56:53 -0700, Michael Torrie <(E-Mail Removed)>
declaimed the following in gmane.comp.python.general:

>
> It's interesting to note that Windows NT sort of descends from VMS. I
> guess the end result was an unholy blend of VMS and CP/M.


Pity they didn't take the "good parts" of VMS...

Interprocess communication via "mailboxes" would have been nice...
More general than UNIX-style "pipes", as once the mailbox has been
created, multiple processes could connect for read or write in parallel.
Processes don't have to be blocked by mailbox I/O...

--
Wulfraed Dennis Lee Bieber AF6VN
(E-Mail Removed) HTTP://wlfraed.home.netcom.com/

 
Reply With Quote
 
J.R.
Guest
Posts: n/a
 
      02-25-2013
On 21/02/2013 19:40, (E-Mail Removed) wrote:

<snip>

> I am nervous about using variables "out of the blue", without having
> to declare them. For example, when I write "i = 0" it is perfectly OK
> to Python without 'i' being declared earlier. How do I know that I
> haven't used this variable earlier and I am unintentionally
> overwriting the value? I find I constantly have to use the search
> facility in the editor, which is not fun.
>
> You see, Javascript, for one, behaves the same way as Python (no
> variable declaration) but JS has curly braces and you know the
> variable you have just used is limited in scope to the code within
> the { }. With Python, you have to search the whole file.
>


No, JavaScript - or better, ECMAScript -, does have variable
declaration. If you don't declare a variable with the "var" statement it
will become an unintentional global variable which is a very bad thing.

If the variable statement occurs inside a Function Declaration, the
variables are defined with function-local scope in that function.
Otherwise, they are defined with global scope (that is, they
are created as members of the global object. Variables are created when
the execution scope is entered. A Block { } does not define a new
execution scope.

PS.: JavaScript is a trademark, and the actual language name is
specified as ECMAScript.

See <http://ecma-international.org/ecma-262/5.1/>

--
Joao Rodrigues (J.R.)
 
Reply With Quote
 
Roy Smith
Guest
Posts: n/a
 
      02-25-2013
In article <kgegna$koj$(E-Mail Removed)>,
"J.R." <(E-Mail Removed)> wrote:

> PS.: JavaScript is a trademark, and the actual language name is
> specified as ECMAScript.


The decision whether to call it JavaScript or ECMAScript really comes
down to, "Do you want to be correct, or do you want people to know what
you're talking about?"
 
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