4 Python functions that make reading easier

A function in Python is a block of coordinated, reusable code that is utilized to play out a solitary, related activity. Functions gives a better seclusion to your application and a serious level of code reusing.

In this post we’re going to see 4 Func…


This content originally appeared on DEV Community and was authored by Dev Write Ups

A function in Python is a block of coordinated, reusable code that is utilized to play out a solitary, related activity. Functions gives a better seclusion to your application and a serious level of code reusing.

In this post we're going to see 4 Functions that make reading Python code easier.

Globals

>>> gloabals()
{'__name__':'__main__', '__doc__': None, ...}

As it name implies, the globals() fucntion will display information of global scope. For example, if we open a Python console and input globals(), a dict including all names and values of variables in global scope will be returned.

Locals

def top_developer(): 
   name = 'DevWriteUps'
   country = 'Canada'
   print(locals())

top_developer()
# {'name':'DevWriteUps', 'country': 'Canada'}

After understanding the globals(), locals() funciton is just a piece of cake. As its name implies, it will return a dict including all local names nad values. By the way, if we call the local() in global scope, the result is identical to globals().

Vars

class TopDeveloper:
  def __init__(self):
   self.name = "DevWriteUps"
   self.county = "Canada"

me = TopDeveloper()
print(vars(me))
# {'name':'DevWriteUps',  'country':  'Canada'}
print(vars(me) is me.__dict__)
# true

The vars() function will return the dict, which is a dictionary used to store an object's attributes. Its result is the same as calling the __dict__ directly.

DIR

class TopDeveloper:
  def __init__(self):
   self.name = "DevWriteUps"
   self.county = "Canada"
me = TopDeveloper()
print(dir(me))


# ['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'country', 'name']

The dir() function helps us demonstrate a lis tof names in the corresponding scope. In fact, the dir method calls __dir__() internally.

Thank you For Reading? Subscribe to our newsletter , we send it occasionally with amazing news, resources and many thing.


This content originally appeared on DEV Community and was authored by Dev Write Ups


Print Share Comment Cite Upload Translate
APA
Dev Write Ups | Sciencx (2023-12-08T02:07:56+00:00) » 4 Python functions that make reading easier. Retrieved from https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/.
MLA
" » 4 Python functions that make reading easier." Dev Write Ups | Sciencx - Friday April 30, 2021, https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/
HARVARD
Dev Write Ups | Sciencx Friday April 30, 2021 » 4 Python functions that make reading easier., viewed 2023-12-08T02:07:56+00:00,<https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/>
VANCOUVER
Dev Write Ups | Sciencx - » 4 Python functions that make reading easier. [Internet]. [Accessed 2023-12-08T02:07:56+00:00]. Available from: https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/
CHICAGO
" » 4 Python functions that make reading easier." Dev Write Ups | Sciencx - Accessed 2023-12-08T02:07:56+00:00. https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/
IEEE
" » 4 Python functions that make reading easier." Dev Write Ups | Sciencx [Online]. Available: https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/. [Accessed: 2023-12-08T02:07:56+00:00]
rf:citation
» 4 Python functions that make reading easier | Dev Write Ups | Sciencx | https://www.scien.cx/2021/04/30/4-python-functions-that-make-reading-easier/ | 2023-12-08T02:07:56+00:00
https://github.com/addpipe/simple-recorderjs-demo