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 845 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…
Parents
  • 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
Comment
  • 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
Children
No Data
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