Python for ethical hackers
Mohammad reza Kamalifard
kamalifard@datasec.ir
Python language essentials
Module 1:
Introduction to Python and Setting up an Environment for Programing
Part 8:
Global and Local Variables
Global and local Variables in Functions
>>> def f():
... print s
>>> s = "I hate spam"
>>> f()
Global and local Variables in Functions
>>> def f():
... print s
>>> s = "I hate spam"
>>> f()
i hate spam
>>>
Global and local Variables in Functions
>>> def f():
... s = "Me too."
... print s
>>> s = "I hate spam."
>>> f()
>>> print s
Me too.
I hate spam.
Global and local Variables in Functions
>>>
...
...
>>>
>>>
>>>

def f():
s = "Me too."
print s
s = "I hate spam."
f()
print s
Global and local Variables in Functions
>>>
...
...
...
>>>
>>>
>>>

def f():
print s
s = "Me too."
print s
s = "I hate spam."
f()
print s
Global and local Variables in Functions
>>> def f():
... print s
... s = "Me too."
... print s
>>> s = "I hate spam."
>>> f()
>>> print s
UnboundLocalError: local variable 's' referenced
before assignment
Global and local Variables in Functions
>>> def f():
... global s
... print s
... s = "That's clear."
... print s
>>> s = "Python is great!"
>>> f()
>>> print s
Global and local Variables in Functions
>>> def f():
... global s
... print s
... s = "That's clear."
... print s
>>> s = "Python is great!"
>>> f()
>>> print s
Python is great!
That's clear.
That's clear.
Global and local Variables in Functions
>>> def f():
... s = "I am globally not known"
... print s
>>> f()
>>> print s
Global and local Variables in Functions
>>> def f():
... s = "I am globally not known"
... print s
>>> f()
>>> print s
I am globally not known
Traceback (most recent call last):
File "global_local3.py", line 6, in <module>
print s
NameError: name 's' is not defined
References
SPSE securitytube training by Vivek Ramachandran
SANS Python for Pentesters (SEC573)
Violent python
Security Power Tools
python-course.eu
----------------------------http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
http://www.python-course.eu/global_vs_local_variables.php
This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit
http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad Reza Kamalifard
All rights reserved.
Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .

جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲

  • 1.
    Python for ethicalhackers Mohammad reza Kamalifard kamalifard@datasec.ir
  • 2.
    Python language essentials Module1: Introduction to Python and Setting up an Environment for Programing Part 8: Global and Local Variables
  • 3.
    Global and localVariables in Functions >>> def f(): ... print s >>> s = "I hate spam" >>> f()
  • 4.
    Global and localVariables in Functions >>> def f(): ... print s >>> s = "I hate spam" >>> f() i hate spam >>>
  • 5.
    Global and localVariables in Functions >>> def f(): ... s = "Me too." ... print s >>> s = "I hate spam." >>> f() >>> print s Me too. I hate spam.
  • 6.
    Global and localVariables in Functions >>> ... ... >>> >>> >>> def f(): s = "Me too." print s s = "I hate spam." f() print s
  • 7.
    Global and localVariables in Functions >>> ... ... ... >>> >>> >>> def f(): print s s = "Me too." print s s = "I hate spam." f() print s
  • 8.
    Global and localVariables in Functions >>> def f(): ... print s ... s = "Me too." ... print s >>> s = "I hate spam." >>> f() >>> print s UnboundLocalError: local variable 's' referenced before assignment
  • 9.
    Global and localVariables in Functions >>> def f(): ... global s ... print s ... s = "That's clear." ... print s >>> s = "Python is great!" >>> f() >>> print s
  • 10.
    Global and localVariables in Functions >>> def f(): ... global s ... print s ... s = "That's clear." ... print s >>> s = "Python is great!" >>> f() >>> print s Python is great! That's clear. That's clear.
  • 11.
    Global and localVariables in Functions >>> def f(): ... s = "I am globally not known" ... print s >>> f() >>> print s
  • 12.
    Global and localVariables in Functions >>> def f(): ... s = "I am globally not known" ... print s >>> f() >>> print s I am globally not known Traceback (most recent call last): File "global_local3.py", line 6, in <module> print s NameError: name 's' is not defined
  • 13.
    References SPSE securitytube trainingby Vivek Ramachandran SANS Python for Pentesters (SEC573) Violent python Security Power Tools python-course.eu ----------------------------http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/ http://www.python-course.eu/global_vs_local_variables.php
  • 14.
    This work islicensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad Reza Kamalifard All rights reserved. Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .