loading...

re: #100DaysOfPython Day 2: Functions, Scope and Best Practices VIEW POST

FULL DISCUSSION
 

Cool post, neat to see Python concepts explained from a JavaScript perspective. I have a few notes / clarifications on some of your points.

  1. Return vs print: the way it was phrased made it seem like the print statement was similar to the return statement. It’s not, print is just a function basically equivalent to console.log.

  2. Scopes: you actually can modify global variables from a local scope by using the global keyword, like this:

global_var = 1

def my_func():
    global global_var
    global_var = 2
    local_var = "a"

    def inner_func():
        nonlocal local_var  # similar to global for inner functions
        local_var = "b"
  1. Arguments: you can have any number of optional arguments (arguments with defaults) after required arguments. There are also a lot more things you can do with arguments (arbitrary args, keyword only, required keywords, positional only) that are useful. They usually involve asterisks and I’m sure you’ll come across them later.
 

These are not meant to be comprehensive tutorials that teach everything about a topic, they're my personal notes that I made into posts. But thanks

code of conduct - report abuse