In the exact scenario you present, you can use default values for arguments, as other answers show.

Generically, you can use the or keyword in Python pretty similarly to the way you use || in JavaScript; if someone passes a falsey value (such as a null string or None) you can replace it with a default value like this:

string = string or "defaultValue"

This can be useful when your value comes from a file or user input:

string = raw_input("Proceed? [Yn] ")[:1].upper() or "Y"

Or when you want to use an empty container for a default value, which is problematic in regular Python (see this SO question):

def calc(startval, sequence=None):
     sequence = sequence or []
Answer from kindall on Stack Overflow
🌐
Boot.dev
boot.dev › lessons › 3963019a-a8ce-45c0-b988-3e6e4121aa36
Learn Python for Beginners: Default Values | Boot.dev
In the function signature, give the armor parameter a default value of 0. Create a damage variable equal to 50 minus the armor.
Discussions

A way to have the default value for a parameter as the default of another function: the Default singleton - Ideas - Discussions on Python.org
Often, I use the defaults of another function in my new function or class. For example: def mysorted(it, key=None, reversed=False): # some code return sorted(it, key=key, reversed=reversed) The majority of times I want to simply adhere to the sign of the original function. More on discuss.python.org
🌐 discuss.python.org
1
September 7, 2022
Is there a way to set a default value for an instance variable?
Since Python allows for optional default values for function and method arguments, that's where you set them: def __init__(self, name="Doug", age=22): More on reddit.com
🌐 r/learnpython
9
3
September 12, 2024
python - How do you set a default value for a variable in Python3? - Stack Overflow
For an assignment I have to write a program that can create directories and files, we have to write a DirectoryEntry class to help do this, and for part of it we have to create a name for the file or More on stackoverflow.com
🌐 stackoverflow.com
October 15, 2016
Python variable value for default argument - Stack Overflow
In Python, what value can a variable take, so that when a function is invoked with the variable as an argument, the function uses its default value for the parameter instead? Consider the following... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Real Python
realpython.com › lessons › default-values-for-variables
Default Values for Variables (Video) – Real Python
00:23 Let’s look at a situation where x is supposed to be a collection we’re supposed to process and we know that variable a is supposed to be a list to work on and variable b, if we don’t have a list, is supposed to be in a dictionary. 00:40 But in the event both of those are empty, we would want to let x be a value to indicate that. So we might use x = a or b or None, and then we can have a test later in the program to see if the value of x is meaningful or whether it has no value at all. 01:07 Another situation, here in the second line, is that there is a default value that we always use for something unless through input or some other action a different value to be used is given.
Published: May 18, 2021
🌐
Home and Learn
homeandlearn.uk › python-functions-defaults.html
Python Default function values
You can set up your Python functions to have default values. Between the round brackets of your function definition, come up with a variable name then type an equals sign.After the equals sign, type a value for your variable. For example, suppose we want our function from the previous lesson to add ten to the answer.
🌐
W3Schools
w3schools.com › python › gloss_python_function_default_parameter.asp
Python Default Parameter Value
If we call the function without argument, it uses the default value: def my_function(country = "Norway"): print("I am from " + country) my_function("Sweden") my_function("India") my_function() my_function("Brazil") Try it Yourself » · Python Functions Tutorial Function Call a Function Function Arguments *args Keyword Arguments **kwargs Passing a List as an Argument Function Return Value The pass Statement i Functions Function Recursion
🌐
Python.org
discuss.python.org › ideas
A way to have the default value for a parameter as the default of another function: the Default singleton - Ideas - Discussions on Python.org
September 7, 2022 - Often, I use the defaults of another function in my new function or class. For example: def mysorted(it, key=None, reversed=False): # some code return sorted(it, key=key, reversed=reversed) The majority of time…
🌐
CodeRivers
coderivers.org › blog › default-variable-value-in-python
Default Variable Values in Python: A Comprehensive Guide - CodeRivers
February 22, 2026 - A default variable value is a value that is automatically assigned to a variable if no other value is provided during its creation or initialization. In Python, this can be applied in various contexts such as function parameters and class attributes.
Find elsewhere
🌐
Rip Tutorial
riptutorial.com › default values for instance variables
Python Language Tutorial => Default values for instance variables
To get a default instance variable ... height, pos=None, color='blue'): self.width = width self.height = height self.pos = pos or [0, 0] # default value is [0, 0] self.color = color r1 = Rectangle2D(5,3) r2 = Rectangle2D(7,8) r1.pos[0] ...
🌐
Reddit
reddit.com › r/learnpython › is there a way to set a default value for an instance variable?
r/learnpython on Reddit: Is there a way to set a default value for an instance variable?
September 12, 2024 -

Basically many years ago I was learning Ruby and I swear there was a way to set a default value when setting up a Class's variables, that could be overridden when creating an instance of a class. And if you didn't specify this instance variable when creating the object then the default class variable would remain. It was basically an optional instance variable.

Is there something similar to this in Python, or am I talking a bunch of nonsense and misremembering?

I'll give an example.

class Animal:

def __init__(self, name, age):

self.name = name

self.age = age

self.animal_type = "fish"

self.location = "ocean"

sammy = Animal("Sammy", 5)

So basically the animal_type and location are class variables, but is there a way for me to add those as optional instance variables, so that they can be changed when creating a new object, or if not specified then the class variable defaults - "fish" and "ocean" - just stay as is?

🌐
TutorialsPoint
tutorialspoint.com › what-is-a-default-value-in-python
Python - Default Arguments
September 15, 2022 - Python allows to define a function with default value assigned to one or more formal arguments. Python uses the default value for such an argument if no value is passed to it. If any value is passed, the default value is overridden with the actual
🌐
Snipe-IT
snipe-it.stanford.edu › home › nude
Default Variable Value In Python - Stanford Snipe Hub
October 18, 2023 - Changing the default value after the function is defined will not affect the variable’s value in already defined functions, but it will affect future function calls. ... If a default value is not provided for a function parameter, Python will raise a TypeError when the function is called without a value for that parameter.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python default parameters
Python Default Parameters
October 5, 2020 - In this syntax, you specify default values (value2, value3, …) for each parameter using the assignment operator (=). When you call a function and pass an argument to the parameter that has a default value, the function will use that argument instead of the default value.
🌐
Bobby Hadz
bobbyhadz.com › blog › python-return-default-value-if-none
How to Return a default value if None in Python | bobbyhadz
Use a conditional expression to return a default value if None in Python, e.g. `return "default value" if my_var is None else my_var`.
🌐
Tecladocode
python.tecladocode.com › treasure hunters › default parameter values
Default Parameter Values | Introduction to Python
Default parameter values don't have to be integers. You can use just about anything as a default value. If it can be passed in as an argument to a function, it's a valid default parameter value. As we've seen, we can pass in variables as arguments.
🌐
GeeksforGeeks
geeksforgeeks.org › python › default-arguments-in-python
Default arguments in Python - GeeksforGeeks
In the first call greet(), no argument is passed, so Python uses the default value "Guest".
Published: March 18, 2026
🌐
Note.nkmk.me
note.nkmk.me › home › python
Default Parameter Values in Python Functions | note.nkmk.me
May 12, 2025 - To avoid this behavior, you can use None as the default value and create a new object inside the function.