Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python as First Langauge
#10
(Jul-04-2020, 02:33 PM)mga010 Wrote: The code creates a warning message on "w=8", indeed, which is good. But why am I able to redefine f()? Is that a good idea to enforce clear and clean code?
There is no Warning message in that code,and there is no return in either function.
The second dos nothing and will return None when do f(w),but will not see that as you only print the list print(w).
To write it so it make some sense.
def f1(v):
    v[4] = 9
    return v

def f2(w):
    w = 8
    return w

if __name__ == '__main__':
    v = list(range(5))
    w = list(range(5))
    print(f1(v))
    print(f2(w))
Output:
[0, 1, 2, 3, 9] 8
So function f2 output 8 as it should,as argument w is never use in that function.

mga010 Wrote:I am not so convinced about the lack of compile-time checking and variable declarations even for beginners.
It is so easy to write "i=..." instead of "j=...", and the resulting errors are very hard to find. In typed languages, "j" would be simply undefined.
The Spyder IDE not even claims about unused but assigned variables in a Python file besides in functions.
You could use Type Annotations as a instruction to types in Python.
It's also good to have this knowledge if they start in larger company's as eg DropBox or Instagram,as using Type Annotations is now is almost mandatory for them.
Our journey to type checking 4 million lines of Python
Instagram has made MonkeyType to add type annotations to code where is not automatically.
So can give a example using code over.
from typing import List, Tuple

def f1(v: List[int]) -> List:
    v[4] = 9
    return v

def f2(w: List[int]) -> None:
    w = 8
    return w

if __name__ == '__main__':
    v = list(range(5))
    v.append('a')
    w = list(range(5))
    print(f1(v))
    print(f2(w))
So we see that function f1 shall take a list of integers as argument,and the return value will be a list.
Have added a problem bye adding string a,but running normal Python will not catch this.
Using a static type checker like mypy will catch it.
So a run look alike this.
E:\div_code
λ python func_1.py
[0, 1, 2, 3, 9, 'a']
8
With mypy it catch all the problems,also all problem as mention with function f2.
E:\div_code
λ mypy func_1.py
func_1.py:8: error: Incompatible types in assignment (expression has type "int", variable has type "List[int]")

func_1.py:9: error: No return value expected
func_1.py:13: error: Argument 1 to "append" of "list" has incompatible type "str"; expected "int"
func_1.py:16: error: "f2" does not return a value
Found 4 errors in 1 file (checked 1 source file)
mga010 Wrote:Anyway, in this first course, the object-oriented approach will only on a basic level. But not understanding those things from the start may backfire later.
As a advice try to learn how Python dos OOP,and not just write as it's done it Java.
Python Is Not Java.
Here a rewrite i did about 4-year ago,someone had written Python as in Java,as they had learn in class.
See the left side is about 3 times shorter Wink
[Image: sHoNd1.jpg]
Reply


Messages In This Thread
Python as First Langauge - by mga010 - Jul-02-2020, 10:09 AM
RE: Python as First Langauge - by Larz60+ - Jul-02-2020, 11:03 AM
RE: Python as First Langauge - by Gribouillis - Jul-02-2020, 11:26 AM
RE: Python as First Langauge - by ndc85430 - Jul-02-2020, 11:53 AM
RE: Python as First Langauge - by mga010 - Jul-02-2020, 03:04 PM
RE: Python as First Langauge - by Larz60+ - Jul-02-2020, 03:16 PM
RE: Python as First Langauge - by Gribouillis - Jul-02-2020, 06:34 PM
RE: Python as First Langauge - by perfringo - Jul-02-2020, 07:39 PM
RE: Python as First Langauge - by mga010 - Jul-04-2020, 02:33 PM
RE: Python as First Langauge - by snippsat - Jul-04-2020, 10:52 PM
RE: Python as First Langauge - by mga010 - Jul-05-2020, 05:34 PM
RE: Python as First Langauge - by voidptr - Jul-25-2020, 04:59 AM
RE: Python as First Langauge - by Larz60+ - Jul-25-2020, 02:00 PM
RE: Python as First Langauge - by mga010 - Jul-27-2020, 04:03 PM
RE: Python as First Langauge - by Gribouillis - Jul-27-2020, 04:30 PM
RE: Python as First Langauge - by mga010 - Jul-28-2020, 04:46 PM
RE: Python as First Langauge - by Larz60+ - Jul-29-2020, 12:08 AM
RE: Python as First Langauge - by voidptr - Aug-02-2020, 07:02 AM
RE: Python as First Langauge - by millpond - Jul-31-2020, 06:49 AM
RE: Python as First Langauge - by elenaflorence87 - Jul-31-2020, 12:20 PM
RE: Python as First Langauge - by anne - Jul-31-2020, 06:06 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020