element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Personal Blogs
  • Community Hub
  • More
Personal Blogs
Clem Martins's Blog New feature in Python 3.8
  • Blog
  • Documents
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: clem57
  • Date Created: 12 Nov 2019 7:18 PM Date Created
  • Views 844 views
  • Likes 5 likes
  • Comments 5 comments
  • python
Related
Recommended

New feature in Python 3.8

clem57
clem57
12 Nov 2019

     Even though Python 3.8 is not official releases, there have been a few Alpha versions floating around. There are some interesting ideas in these including one called a Walrus Operator.

Here is the explanation:

 

Walrus-operator is another name for assignment expressions. According to the official documentation, it is a way to assign to variables within an expression using the notation NAME := expr. The Assignment expressions allow a value to be assigned to a variable, even a variable that doesn’t exist yet, in the context of expression rather than as a stand-alone statement.

This can make a longer expression be shortened with in the context of the assignment. For example:

sample_data = [ 
    {"userId": 1,  "name": "rahul", "completed": False}, 
    {"userId": 1, "name": "rohit", "completed": False}, 
    {"userId": 1,  "name": "ram", "completed": False}, 
    {"userId": 1,  "name": "ravan", "completed": True} 
] 

print("With Python 3.8 Walrus Operator:")  
for entry in sample_data:  
    if name := entry.get("name"): 
        print(f'Found name: "{name}"')

 

Here is the same without using Walrus:

 

print("Without Walrus operator:")

for entry in sample_data:
    name =entry.get("name")
    if name:
        print(f'Found name: "{name}"')

 

Please comment if you thing this is a good change or not. Discuss the reasons why or why not...

  • Sign in to reply

Top Comments

  • ntewinkel
    ntewinkel over 5 years ago +5
    That seems to be a convergence to what other languages are doing. The same thing in Swift looks like this: let sample_data: [[String: Any]] = [ ["userId": 1, "name": "rahul", "completed": false], ["userId…
  • skywalker1211
    skywalker1211 over 5 years ago +2
    at first sight, it looks like comparison operator. it saves one line, with reduce readability. i will still stick with the one without walrus operator. i dont know if it is a good change, may be someday…
  • neilk
    neilk over 5 years ago in reply to ntewinkel +1
    ntewinkel wrote: I kind of prefer the readability of keeping things separate. Going back to the days of C, I recall some programmers chunking entire functions into a single line which only made sense to…
  • clem57
    clem57 over 5 years ago

    I learned to program in the early 70s - ALGOL on an Elliot 803!

     

    My teachers drummed into me the importance of readability, simplicity of expression and comments.

     

    I have never forgotten that.

     

    neilk

    I agree about readability, but the new style of camel case, etc just does not come too easily. Oh well, I will just have to adjust as best I can.image

    Clem

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • skywalker1211
    skywalker1211 over 5 years ago

    at first sight, it looks like comparison operator.

    it saves one line, with reduce readability. i will still stick with the one without walrus operator.

    i dont know if it is a good change, may be someday i will need it.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • neilk
    neilk over 5 years ago in reply to ntewinkel

    ntewinkel  wrote:

     

     

    I kind of prefer the readability of keeping things separate.

     

    Going back to the days of C, I recall some programmers chunking entire functions into a single line which only made sense to the most advanced programmers but made it completely illegible to anyone else.

    To me, that's bad style. At some point a junior programmer will need to read your code. Or worse - you might need to read your OWN code a few years later to support it image

     

    I learned to program in the early 70s - ALGOL on an Elliot 803!

     

    My teachers drummed into me the importance of readability, simplicity of expression and comments.

     

    I have never forgotten that.

     

    Neil

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 5 years ago

    I can see the utility in testing incomplete programs.

     

    DAB

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • ntewinkel
    ntewinkel over 5 years ago

    That seems to be a convergence to what other languages are doing.

     

    The same thing in Swift looks like this:

     

    let sample_data: [[String: Any]] = [
        ["userId": 1,  "name": "rahul", "completed": false],
        ["userId": 1, "name": "rohit", "completed": false],
        ["userId": 1,  "name": "ram", "completed": false],
        ["userId": 1,  "name": "ravan", "completed": false]
    ]
      
    print("Swift output")
    for entry in sample_data {
        if let name = entry["name"] {
            print("found name: \(name)")
        }
    }

     

    With lots of subtle and annoying syntax differences between all languages. arg.

     

    I kind of prefer the readability of keeping things separate.

    It also allows you to easily add a debug log statement in between the assignment and the if statement, if something comes up in the future.

     

    Going back to the days of C, I recall some programmers chunking entire functions into a single line which only made sense to the most advanced programmers but made it completely illegible to anyone else.

    To me, that's bad style. At some point a junior programmer will need to read your code. Or worse - you might need to read your OWN code a few years later to support it image

     

    What's the reason for so many languages anyway? To me it seems to be very political, including patent pirates making life difficult.

    For example, it looks like for Android development, Java is being replaced by Kotlin now because Oracle is starting to charge for the use of Java.

    I might be interpreting that Kotlin move wrong.

     

    Of course, I guess there's always just the fact that many people have many different preferences.

     

    Cheers,

    -Nico

    • Cancel
    • Vote Up +5 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube