Giang Nguy (@GiangNguy1) header
Giang Nguy (@GiangNguy1) profile picture

I'm a Vietnamese banker-turned-English teacher. I love learning new things and exploring life, which partly justifies my love for the English language.

Hanoi, Vietnam
Joined June 2019
Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)Photo by Giang Nguy (@GiangNguy1)
A wrapper function is a function that calls another function (the wrapped function) to add some extra functionality or modify its behavior. Think of it as an outer layer around an existing function. Wrapper functions are useful when you want to add functionality to an existing function without modifying its original code. For example, we have a simple function: def greet(name): print(f"Hello, {name}!") Let's create a wrapper function to add some additional context before and after the greeting: def greet_with_intro(name): print("Welcome!") greet(name) print("Have a great day!") Now, if we call the wrapper: greet_with_intro("Alice"), the output will be: Welcome! Hello, Alice! Have a great day! Here, greet() is the original (wrapped) function. greet_with_intro() is the wrapper function. The wrapper calls the original function but adds additional instructions before and after it. -------- A wrapper function is one of the main building blocks of Python decorators. A decorator is a function that takes another function as an argument and returns an enhanced or modified version of that function without changing its original source code. A decorator allows you to add extra functionality to an existing function without modifying the function itself. The attached picture shows a simple example of a decorator called 'my_decorator'. In this example, 'my_decorator' is the decorator function, 'func' is a parameter that receives another function as an argument. We define an inner wrapper function called wrapper(). This wrapper function does pretty much the same thing as the wrapper greet_with_intro() above: 1) Prints a message before the original function executes 2) Calls func(), which is the original function passed to the decorator 3) Prints another message after the original function finishes executing The decorator returns the wrapper function: "wrapper" refers to the function object itself. We apply a decorator to a function by writing @decorator_name immediately before the function definition (with no unrelated statement in between). In our example, @my_decorator applies the decorator to greet(). This is actually equivalent to writing: def greet(name): print(f"Hello, {name}!") greet = my_decorator(greet) Here, Python creates the original greet() function and passes that function object to my_decorator. my_decorator then returns the wrapper function. The name 'greet' is now bound to the returned wrapper function. Therefore, when we call: greet("Alice"), we're actually executing wrapper(). We can also apply the same decorator to multiple functions. For example: @my_decorator def inquire(name): print(f"What are you looking for, {name}?") inquire("Bob") Output: Welcome! What are you looking for, Bob? Have a great day! -------- But there's another point we need to make clear: If a wrapper function can already add extra functionality to an existing function, why do we need a decorator at all? Why not simply call the wrapper function directly? For example, I can write: def wrapper(func, name): print("Welcome!") func(name) print("Have a great day!") Now define some original functions: def greet(name): print(f"Hello, {name}!") def inquire(name): print(f"What are you looking for, {name}?") I can still use the same wrapper for both: wrapper(greet, "Alice") wrapper(inquire, "Bob") Output: Welcome! Hello, Alice! Have a great day! Welcome! What are you looking for, Bob? Have a great day! However, with this approach, every time you want to execute the enhanced function, you must explicitly call wrapper() and pass the original function as an argument. With the decorator approach, after decoration, 'greet' itself refers to the wrapper function returned by the decorator. Therefore, you can simply write: greet("Alice"). The decorator mechanism allows the enhanced function to be called using its original name.
16
Happy Thanksgiving to my role model @lexfridman, my dear friend @sandrag926, and others on X who celebrate this holiday 🥘🍲🍂 Lex is traveling, so this year he won’t get to have a cozy feast with his family? I hope he shares his location with his mom regularly so she won’t worry too much.
1
1
79
In this sample code (attached pic, or at 43:20 of the YouTube lecture, link below), the lecturer was using super() in the implementation of RunWorkout to invoke the __init__ method of Workout and said that super() returned Workout - the parent class itself. This is incorrect. In fact, super() returns a proxy object (a super object), not the parent class of the class being defined. In more detail, super() is built-in function that returns a proxy object allowing you to access methods from the next class in the Method Resolution Order (MRO). This is a very important point. Using super() doesn't simply mean "call my parent class", it means: call the next class in the MRO. So what is MRO? Every class has its own MRO, determined by its inheritance hierarchy. When Python creates a class, it also calculates the class's MRO. This will be the order in which Python searches classes for a method or attribute when dealing with inheritance, especially multiple inheritance (a subclass having two or more parent classes). MRO tells Python: "If I call a method on an object of this class, which class should I look in first, second, third, and so on?" super() follows the MRO of the class of the object/instance on which the method is called. Let's illustrate this with an example: class A: def speak(self): print("A speaks.") class B(A): def speak(self): print("B speaks.") super().speak() class C(A): def speak(self): print("C speaks.") super().speak() class D(B, C): pass d = D() d.speak() Output of the code is: B speaks. C speaks. A speaks. Why? First, let's examine the MRO of D: print(D.mro()) # [D, B, C, A, object] Here's what happens: 1. D doesn't define speak(), so Python finds it in B. 2. B.speak() executes. Its super() continues to C because C is the next class in D's MRO. 3. C.speak() executes. Its super() continues to A. 4. A.speak() executes. ------ Link to the YouTube lecture: youtube.com/watch?v=-wyc5Fwz…
95
I think @lexfridman might be interested in reading Mr. Ngo Bao Chau's perspective on the Riemann hypothesis being solved with the help of AI. He's a renowned Vietnamese mathematician and a 2010 Fields Medalist. Our pride 💖
1
119
Now that @lexfridman is out enjoying the great outdoors and commenting on girls’ posts, he should also update his status sometimes so that his fans feel reassured about his situation.
1
97
Dictionaries are useful for counting word frequencies, which is a common programming exercise. However, we can also use the get() method to shorten the code, rather than checking whether each word in the string already exists in the dictionary we're building. Notice how count_words1() performs fewer operations than count_words() while producing the same result. get(key, default=None) returns the value associated with a key. If the key doesn't exist, it returns the default value instead of raising a KeyError. Compare this with direct indexing: student["grade"] raises a KeyError if the key 'grade' doesn't exist in the dictionary 'student'. This is one of the main advantages of get()
1
72
I love seeing pictures of Crown Prince Mohammed bin Salman in his office. He's such a hardworking and responsible leader 🥰 Some people even managed to identify the bottle of perfume on his desk. You can also see a bit of the bottle in the second photo of the post I retweeted. The Crown Prince is indeed a man of class and refined taste. I'm sure the fragrance of His Handsome Highness is irresistible, with or without his favourite perfume 🥺
3
11
111
75,230
Thanks to the reform initiatives spearheaded by Crown Prince Mohammed bin Salman, more Saudi women are joining the workforce and contributing to the development of their country. Saudi female students are also achieving remarkable academic success. I'm Vietnamese, yet I feel proud of these achievements because I admire the Crown Prince's vision and his accomplishments in transforming his country 🥰 @KingSalman @HRHMBNSALMAAN @KSAmofaEN @spagov @Spa_Eng @Badermasaker @Bandaralgaloud @TheMoShowPod @r___66e @ksaembassyvn
1
2
428
I need to issue a correction for this tweet. In the first sentence, I meant function gcd(), not function reduce()
I was learning this piece of code and encountered the Euclidean algorithm, which is used to find the greatest common divisor (GCD) of two integers without having to list all their factors (function reduce() in the code). Learning to code allows me to brush up on my math knowledge as well 🥰 The key mathematical principle behind the Euclidean algorithm is: gcd(a, b) = gcd(b, a mod b). In other words, the GCD of two integers is equal to the GCD of the smaller integer and the remainder when the larger integer is divided by the smaller one. You repeat this process until the remainder becomes zero. The last nonzero remainder is the GCD. Let's prove this algorithm. Suppose: a > b and a = bq + r where q is the quotient and r is the remainder. Any integer that divides both a and b must also divide r, because r = a - bq. => Every common divisor of a and b is also a common divisor of b and r (1) Conversely, any integer that divides both b and r must also divide a, because a = bq + r => Every common divisor of b and r is also a common divisor of a and b (2) From (1) and (2), we see that the two pairs (a, b) and (b, r) have exactly the same set of common divisors. Consequently, their greatest common divisors must be equal: gcd(a, b) = gcd(b, r) We can also prove that the last nonzero remainder gives the GCD. Start with two positive integers a > b and apply the property gcd(a, b) = gcd(b, r) repeatedly: a = bq₁ + r₁ b = r₁q₂ + r₂ r₁ = r₂q₃ + r₃ ... rₙ₋₂ = rₙ₋₁qₙ + rₙ rₙ₋₁ = rₙqₙ₊₁ + 0 We have: gcd(a, b) = gcd(b, r₁) = gcd(r₁, r₂) = ... = gcd(rₙ₋₁, rₙ). The final division tells us that rₙ divides rₙ₋₁. Therefore, gcd(rₙ₋₁, rₙ) = rₙ. Hence, gcd(a, b) = rₙ
1
251
I wish @lexfridman would share pictures from his motorcycle journey with us from time to time. We have no idea where he is right now or whether he's traveling alone. And I hope his loved ones are able to keep track of his whereabouts. @gregfridman
1
214
I was learning this piece of code and encountered the Euclidean algorithm, which is used to find the greatest common divisor (GCD) of two integers without having to list all their factors (function reduce() in the code). Learning to code allows me to brush up on my math knowledge as well 🥰 The key mathematical principle behind the Euclidean algorithm is: gcd(a, b) = gcd(b, a mod b). In other words, the GCD of two integers is equal to the GCD of the smaller integer and the remainder when the larger integer is divided by the smaller one. You repeat this process until the remainder becomes zero. The last nonzero remainder is the GCD. Let's prove this algorithm. Suppose: a > b and a = bq + r where q is the quotient and r is the remainder. Any integer that divides both a and b must also divide r, because r = a - bq. => Every common divisor of a and b is also a common divisor of b and r (1) Conversely, any integer that divides both b and r must also divide a, because a = bq + r => Every common divisor of b and r is also a common divisor of a and b (2) From (1) and (2), we see that the two pairs (a, b) and (b, r) have exactly the same set of common divisors. Consequently, their greatest common divisors must be equal: gcd(a, b) = gcd(b, r) We can also prove that the last nonzero remainder gives the GCD. Start with two positive integers a > b and apply the property gcd(a, b) = gcd(b, r) repeatedly: a = bq₁ + r₁ b = r₁q₂ + r₂ r₁ = r₂q₃ + r₃ ... rₙ₋₂ = rₙ₋₁qₙ + rₙ rₙ₋₁ = rₙqₙ₊₁ + 0 We have: gcd(a, b) = gcd(b, r₁) = gcd(r₁, r₂) = ... = gcd(rₙ₋₁, rₙ). The final division tells us that rₙ divides rₙ₋₁. Therefore, gcd(rₙ₋₁, rₙ) = rₙ. Hence, gcd(a, b) = rₙ
1
2
406
Btw my role model @lexfridman you can fall in love and have babies, but please don’t get married 😠 Haven’t you learned anything from the Jensen case at all?? I’m not going to get married to any man whatsoever, even to the Crown Prince of Saudi Arabia who is absolutely adorable as well. What if I become a billionaire in the future, who knows, or I become infatuated with another man? I need to have the option of walking away without losing my large fortune 😠
1
160
But this is a very long loooong journey 🥹 Maybe @lexfridman should take frequent breaks and make time to get to know his fans in Asia, who have always supported him wholeheartedly. I'm his biggest fan in Vietnam, how come I haven't had the chance to see my role model in person?
1
126
Please stay safe on the road @lexfridman 🙏🙏🍀 Your fans have been missing you but you don’t know about that 😔
1
120
A leader who is willing to help those who are just starting out in their careers is truly admirable ❤️
This quoted post is unavailable.
1
150
Onto the next great thing 🍀 Forget and keep moving 💪 @JensenHuang @lexfridman
Months before the scandal, Jensen Huang explained exactly how he deals with embarrassment The Nvidia CEO admitted he hits low points “all the time”, and his answer is to forget and keep moving “A lot of it is you gotta be tough on yourself. Come on, stop crying about it. Let’s get going” “The embarrassments are gonna surprise you, the humiliations are gonna surprise you” “Forget about it, move on, keep moving”
2
253
Crown Prince Mohammed bin Salman appeared cheerful and in good spirits yesterday as he went about his usual duties as Saudi Arabia's leader, even though we all know that the conflict involving the Houthis is still ongoing. I've always had the impression that the Crown Prince is an optimistic and cheerful person. His smile is genuine and contagious, and it has been a constant source of joy and comfort to many Saudis, his fans around the world, and indeed anyone who follows Middle Eastern politics. I believe that with this positive attitude, Crown Prince MBS will find ways to overcome the current Houthi threat. There's a solution to every problem, and inshallah, a sustainable solution to this one will emerge soon. The second picture shows some lovely words I found on a chocolate bar I bought in the Netherlands back in September. (I've recently cut out sweets as part of my efforts to improve my health and appearance.) I kept the wrapper as a souvenir because of these beautiful words: "I achieve my goals with joy and laughter." I think that's just the right attitude to have when facing any challenge, and it's also something I've learned from following Crown Prince MBS. I wish him and the people of Saudi Arabia all the very best ❤️ @KingSalman @HRHMBNSALMAAN @KSAmofaEN @Badermasaker @Bandaralgaloud @FaisalbinFarhan @kbsalsaud @arabnews @spagov @Spa_Eng @TheMoShowPod @r___66e @ksaembassyvn
31
67
4
389
67,590