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)
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)
Example
has three types of methods and variables:_
prefix and are typically considered non-public, although Python does not enforce this.__
prefix and are only accessible within the class itself.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
.type()
to know the type of a variable for example:type(123) # <class 'int'>
123_145_877
pass
in Python functions as a placeholder for empty code.pass
prevents errors.pass
can be used to intentionally skip a block of code if a certain condition isn't met.%pip
and %conda
command that will run in the current kernel.%pip install geocoder
import sys
!{sys.executable} -m pip install geocoder