Sunday, 26 March 2023

5 awesome hidden features in Python

 Walrus operator (:=): This operator allows you to assign and return a value in the same expression. It can be particularly useful in list comprehensions or other situations where you need to assign a value to a variable and use it in a subsequent expression. Here's an example:

if (n := len(my_list)) > 10:

    print(f"List is too long ({n} elements, expected <= 10)")


Extended Iterable Unpacking: This feature allows you to unpack an iterable into multiple variables, including a "catch-all" variable that gets assigned any remaining items in the iterable. Here's an example:

first, *middle, last = my_list

In this example, first is assigned the first item in my_list, last is assigned the last item, and middle is assigned all the items in between.


Underscore as a placeholder: In interactive Python sessions, you can use the underscore (_) as a shorthand for the result of the last expression. This can be useful if you need to reuse the result of a previous command. Here's an example:

>>> 3 * 4
12
>>> _ + 5
17

slots attribute: The __slots__ attribute allows you to define the attributes of a class and their data types in advance, which can make the class more memory-efficient. Here's an example:

class MyClass:
    __slots__ = ("x", "y")
    
    def __init__(self, x, y):
        self.x = x
        self.y = y

In this example, we are defining a class with two attributes (x and y) and using the __slots__ attribute to define them in advance.

Callable instances: In Python, instances of classes can be made callable by defining a __call__ method. This can be useful if you want to create objects that behave like functions. Here's an example:

class Adder:
    def __init__(self, n):
        self.n = n
        
    def __call__(self, x):
        return self.n + x
    
add_five = Adder(5)
result = add_five(10)
print(result)  # Output: 15


In this example, we are defining a class Adder that takes a number n and defines a __call__ method that adds n to its argument. We then create an instance of Adder with n=5 and use it like a function to add 5 to 10, resulting in 15.

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (53) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (932) Python Coding Challenge (358) Python Quiz (23) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses