@folkerschamel Some may say it's deliberate flexibility, but I think the trend is to use each one more consistently. But yes, there are many exceptions out there, including in the docs!
#newbie question: What are the pros of autodeleting toots? Only people who browse you profile will ever see them after the initial posting and any boosts, so why not leave them there?
Later this week, I'll look at other aspects relating to #Python #functions that can confuse those beginners moving on to more intermediate levels, including:
• _positional and named arguments_
• _optional arguments_ (by defining default values)
• the "obscure" _args and kwargs_
• forcing _positional-only_ or _keyword-only_ arguments using the "rogue" forward slash `/` and asterisk `*` in the function signature
• _type-hinting_ when defining functions
• some best practices when defining and using functions
Some of these topics I've written about a while ago in this Real Python article: https://realpython.com/python-optional-arguments/
"What's in a name?", one might argue? And there's a point. What matters most is knowing how to use concepts rather than knowing the precise definitions
Still, these terms are used everywhere-in documentation, in tutorials, when talking to other programmers
So, knowing the terminology _is_ important. It shouldn't be the first thing someone learns, but eventually, everyone should become familiar with the right terms for the right things
/6
# — Argument —
An _argument_ is the actual information you send to a function when you call it
You called the function twice in the example above
The first time you called `greet_person()` you used the argument `"Ishaan"` and the second time `"Elizabeth"`
When you call the function, the information (the argument) is stored in a variable named `person` inside the function
Don't worry too much if you confuse parameters and arguments. Many programmers confuse them, too!
/5
# — Parameter —
A _parameter_ is the name you choose for information that's needed by the function
You add parameters inside the brackets in the function signature which is the line which includes `def`
In this example, the parameter is `person`
This is the name of the "storage box" which is ready to hold any information you send into the function
However, when you define the function, this "box" is still empty…
/4
# — Call —
You _call_ a function when you use it
You're calling a function when you write its name followed by parenthesis (the round brackets)…
The code in the function definition will run when you call a function
In the example above, you're calling the function twice on the last two lines of the code
/3
# — Define —
You _define_ the function when you use the `def` keyword
The first line of the definition is the function signature and the code after the colon is the code you want the function to perform…
However, this code does not run when you define a function
For this, we need to call the function…
/2
# Functions in Python—easy or hard to learn & master?
This week I'll be looking at aspects of functions which can be tricky for those who know basics and want to move to next level. If you've learnt about functions in Python are keen to take the next step, then hopefully this series of toots will help…
Let's start with the unexciting but important—terms
• _define_
• _call_
• _parameter_
• _argument_
Here's the code we'll use as an example which you can also get from the ALT text of the image in this toot:
`def greet_person(person):`
`print(f"Hello {person}! How are you doing today?")`
`greet_person("Ishaan")`
`greet_person("Elizabeth")`
You can follow this thread in the replies (unlisted)
/1
#Python #functions #coding #programming #LearnToCode #LearnPython
@hynek @asmodai @glyph this set up is more prone to acrimony than Twitter. Twitter has one moderation policy, whether you agree with it or not, carried out by “anonymous” Twitter staff. Here, the moderators are public and all do their own thing. This can only lead to more fracturing.
The model here will make social media bubbles a lot worse for those who choose overtly political servers who block any server with a different political view
I only interact with Python/programming people both on Twitter and here, so never had issues as they’re very friendly communities everywhere. But those who use these platforms for other topics will become more isolated from other views, not less
@Dwflanagan also, the instance I’m on has markdown as an option for writing toots, which although not as good as full syntax formatting, is good enough to create a simple code block. Unfortunately at the moment I think it’s one if the few and only those on the same server see the markdown version, everyone else sees plain text
@Dwflanagan adding the code as the ALT text of an image snippet is a very good solution that I’ve been using elsewhere and will be using here
@allenellis FWIW, this is what it appears for those on the #qoto server – but it makes it a bit pointless to create toots with markdown when most people will see a plain text version of it!
@allenellis I’ve since discovered only people on my server (and any others that implement markdown) see the markdown. Everyone else sees the plain text version. Which defeats the purpose for me!
@peterdrake @chris @s_gruppetta right!
I usually explain to my students by noting that __iadd__ stands for in-place addition and in-place operations are intended to mutate the original object when possible.
Though += and friends are most often used on strings and numbers, which are immutable so we don't see that behavior most of the time...
I do wonder whether it might have been more sensible to design Python to never mutate with augmented assignments, even on mutable objects.
@peterdrake @chris As @s_gruppetta said, it's all about how += works on lists.
+= on mutable objects is a combination of __iadd__ and =,
The __iadd__ call succeeds but the assignment fails
Interestingly += on tuples doesn't work this way! The += operator falls back to __add__ followed by an assignment on immutable objects.
So while
a += b
Is the same as
a = a + b
With a tuple or a string.
Those two lines aren't the same on a list!
This oddity is in the docs too: https://docs.python.org/3.5/faq/programming.html#faq-augmented-assignment-tuple-error
@peterdrake @chris @treyhunner This shows that `+=` is not quite just syntactic sugar for the separate `=` and `+`. One creates a new object; the other doesn't
@peterdrake @chris @treyhunner Oops, markdown error (shame there's no preview), the id for `b` in the first example should be on a new line, of course
@peterdrake @chris @treyhunner Trey is the master at these things, but here's my attempt.
In the first one, the expression after the `=` is executed first; this is fine as you're adding one list to another (using `list.__add__()`). This creates a new object. Then, there's an attempt to assign this new object as the first item in `a` which fails since `a` is a tuple.
In the second, the assignment and addition are attempted together using the `__iadd__()` dunder method, rather than `__add__()`. No new object is created.
Here's a different example which may shed some light:
`>>> b = [2]`
`>>> c = [3]`
`>>> id(b)`
`4407498688`
`>>> b = b + c`
`>>>`id(b)`
`4407508864`
`b` has a different id after the assignment, so it's a different object to the original `b`
now, in a new session:
`>>> b = [2]`
`>>> c = [3]`
`>>> id(b)`
`4394855808`
`>>> b += c`
`>>> id(b)`
`4394855808`
`b` is now the same object as it was before (same id)
@treyhunner Each time I see this I still have to stop and look at it again, despite having seen it before!
• Rethinking how to teach programming – I prefer the friendly, relaxed approach when communicating about Python programming
• I write about Python on The Python Coding Book blog and on Real Python
• Former Physicist
• Expect posts on scientific and numerical programming –> NumPy, Matplotlib and friends!