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

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
Setting a default value for a variable in function in python - Stack Overflow
Here I have a function def myfunc(some_text, argument): print(some_text) if argument == True: print("Argument is True") But I want it make it so that if I dont type anyth... More on stackoverflow.com
🌐 stackoverflow.com
python - Set default value and type for parameter - Stack Overflow
How can I do something like this: def profile(request, pk=0 : int): #to do I need pk to be int (without converting in function). Like this: def profile(request, pk: int): If pk empty - set ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
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…
🌐
Real Python
realpython.com › lessons › default-values-for-variables
Default Values for Variables (Video) – Real Python
01:57 On the other hand, if they didn’t provide a value for a, we somehow indicate that with an empty string (""). And then when we say y = a or default, a evaluates to be False, so whatever is in default is returned. 02:17 In this case, we’ll have our workweek beginning on 'Sunday'. Become a Member to join the conversation. ... Get a Python Cheat Sheet (PDF) and learn the basics of Python, like working with data types, dictionaries, lists, and Python functions:
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 ...
🌐
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?

Find elsewhere
🌐
pythontutorials
pythontutorials.net › blog › how-to-set-default-value-for-variable
How to Set Default Values for Variables in Python: A Guide Compared to JavaScript's || Operator — pythontutorials.net
It’s useful for setting defaults in concise, readable code. some_value = None x = some_value if (some_value := some_value) is not None else 5 # x = 5 · # Default to 5 if some_value is None, else use some_value x = 5 if (some_value := None) is None else some_value # x = 5 · While not always necessary, := shines in complex conditionals where you want to avoid重复赋值. To highlight how Python’s tools compare to JavaScript’s ||, let’s summarize key differences:
🌐
Rip Tutorial
riptutorial.com › default values for instance variables
Python Language Tutorial => Default values for instance variables
This behavior is caused by the fact that in Python default parameters are bound at function execution and not at function declaration. To get a default instance variable that's not shared among instances, one should use a construct like this: class Rectangle2D(object): def __init__(self, width, 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] = 4 r1.pos # [4, 0] r2.pos # [0, 0] r2's pos hasn't changed
🌐
W3Schools
w3schools.com › python › gloss_python_function_default_parameter.asp
Python Default Parameter Value
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... The following example shows how to use a default parameter value.
🌐
Note.nkmk.me
note.nkmk.me › home › python
Default Parameter Values in Python Functions | note.nkmk.me
May 12, 2025 - You can set default values in a function definition using the format parameter_name=default_value. If the corresponding argument is omitted during the function call, the default value will be used. def func_default(arg1, arg2='default_x', ...
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › python › default-arguments-in-python
Default arguments in Python - GeeksforGeeks
If you provide a value, Python uses it. If you skip it, the default value is used automatically.
Published: March 18, 2026
🌐
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. Regular variable assignments require an ...
🌐
Stack Abuse
stackabuse.com › default-arguments-in-python-functions
Default Arguments in Python Functions
March 16, 2020 - Default arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call. Let's define a function with one default argument. def find_square(integer1=2): result = integer1 * integer1 return result · The above script defines a function find_square() with one default argument i.e. integer1. The default value for the integer1 argument is set to 2.
🌐
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.
🌐
W3Schools
w3schools.com › python › ref_dictionary_setdefault.asp
Python Dictionary setdefault() Method
Python Examples Python Compiler ... car.setdefault("model", "Bronco") print(x) Try it Yourself » · The setdefault() method returns the value of the item with the specified key....