menu

Search By Label

from functools import lru_cache
@lru_cache(maxsize= 128) #cache can save up to 128 entries and optimize execution
def fib_with_cache(n): 
   if n < 2: 
       return n 
   return fib_with_cache(n-1)+ fib_with_cache(n-2) 
There is not something like a keyword to user encapsulation but you can address the same behavior by using Python convention "_" and "__"

class Example:
    def __init__(self):
        self.public_var = "public"
        self._protected_var = "protected"
        self.__private_var = "private"

    def public_method(self):
        print("This is a public method")

    def _protected_method(self):
        print("This is a protected method")

    def __private_method(self):
        print("This is a private method")

# Creating an instance of the class
example_instance = Example()

# Accessing public method and variable
example_instance.public_method()
print(example_instance.public_var)

# Accessing protected method and variable
example_instance._protected_method()
print(example_instance._protected_var)

# Trying to access private method and variable (will raise AttributeError)
# example_instance.__private_method()
# print(example_instance.__private_var)

The class Example has three types of methods and variables:
  • Public methods and variables are accessible from outside the class.
  • Protected methods and variables have a single underscore _ prefix and are typically considered non-public, although Python does not enforce this.
  • Private methods and variables have a double underscore __ prefix and are only accessible within the class itself.

In the example code:
  • public_var, public_method(), _protected_var, and _protected_method() are respectively public and protected variables and methods.
  • _protected_var and _protected_method() can be accessed from outside the class but are typically considered non-public.
  • __private_var and __private_method() are private variables and methods and can only be accessed within the class itself. Trying to access them from outside the class will raise an AttributeError.
You can use the method type() to know the type of a variable for example:
type(123) # <class 'int'>
Using Python you can separate numbers with "_" value for example:
123_145_877
This does not affect the value or type of the number, it is only for improving human reading!
We use pass in Python functions as a placeholder for empty code.
It basically tells Python to do nothing in that specific spot. It's useful for:

  • Marking incomplete functions: When you're defining a function but haven't written its logic yet, pass prevents errors.
  • Skipping code sections: Within a function, pass can be used to intentionally skip a block of code if a certain condition isn't met.
In IPython 7.3 and later, there is a magic %pip and %conda command that will run in the current kernel.

%pip install geocoder

In earlier versions, you need to use sys to fix the problem like in the answer by FlyingZebra1

import sys
!{sys.executable} -m pip install geocoder