Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6663161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:28:07+00:00 2026-05-26T02:28:07+00:00

About swap , in C++, we can swap two by std::swap(x, y); . x

  • 0

About swap, in C++, we can swap two by std::swap(x, y);.
x and y are passed into swap as reference.

But in Python, can we also write a swap function which can do the swap the same way as in C++?
The Python version of swap I implemented is kinda stupid:

def swap_in_python(x, y):
    return y, x

#so when swapping a and b, I usually do it like this
a, b = swap_in_python(a, b)

This is really cumbersome, but we can’t pass reference or pointer as what we can do in C++, right?

EDIT:

Is there any other way not employing assignment(not using =)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:28:07+00:00Added an answer on May 26, 2026 at 2:28 am

    Actually, in Python, it’s much more elegant:

    (a,b) = (b,a)
    

    You don’t have to call a function at all. And, yes, as per the comments, the parentheses are unnecessary here. I still prefer them since I believe they make the intent clearer.


    Following edit:

    Is there any other way not employing assignment (not using =)?

    Possibly, though I don’t know of one, and I can’t imagine it would be simpler or more readable than the basic way shown above. If you’re looking for a more complicated way of doing things that can be done simply, might I suggest that you’re in the wrong mindset? 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was asking b4 about swapping query to swap data between rows in same
I could not sleep last night and started thinking about std::swap . Here is
Firstly sorry about my grammar. I have two ImageViews and I want to swap
I've read about this a lot and I just can't figure it out. It
i was reading about how you can only pass by value in java and
The function CreateFileMapping can be used to allocate space in the pagefile (if the
Why has std::swap been moved to the <utility> header for C++11? N3290 C.2.7 says:
I am developing software in C#/.NET but I guess the question can be asked
I'm trying to single source a form page which can be in edit mode
Say I have two std::set<std::string> s. The first one, old_options , needs to be

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.