Questions about this topic? Sign up to ask in the talk tab.
Classes/Logs/2012/September/18/01-02
From NetSec
Revision as of 03:20, 18 September 2012 by Chantal21I (Talk | contribs)
01:16:41 <zzzzzZZZZzzz> Who listened to my last class? 01:17:06 <zzzzzZZZZzzz> First of all. 01:17:59 <zzzzzZZZZzzz> We left off with python functions 01:18:28 * l0wtech has quit (Client exited) 01:18:38 * l0wtech ([email protected]) has joined #CSIII 01:19:18 <zzzzzZZZZzzz> http://pastebin.com/3dG6iygw this piece of code 01:20:40 <d4rch0n> it might be better to start with #!/usr/bin/env python 01:20:58 <zzzzzZZZZzzz> http://pastebin.com/9udApQq9 was the logs 01:21:01 <zzzzzZZZZzzz> Yes, fair 01:21:39 * alphis has quit (Ping timeout) 01:21:49 <zzzzzZZZZzzz> I guess I should disclaimer myself as not a python god, or god of any matter, and for that I may leave rough edges, but I will do my best, please assist me :) 01:22:54 <d4rch0n> lol np. me neither, but i'll let you know if i catch anything 01:22:59 <zzzzzZZZZzzz> ^^ 01:24:48 <zzzzzZZZZzzz> So if I begin from last time, I think we should start explaining lines 5, and 6. 01:25:13 <zzzzzZZZZzzz> Which is the function definition for the 'main' function. 01:26:25 <zzzzzZZZZzzz> Well 01:26:38 <zzzzzZZZZzzz> I should say it is the definition and code body for the main() function 01:26:44 <hatter> <3 01:26:46 <zzzzzZZZZzzz> the definition being line 5 01:26:51 <zzzzzZZZZzzz> and 6 being the code body 01:27:42 <zzzzzZZZZzzz> we already know what print does and how it concatenates variables, etc, so lets focus more on what is happening with the function it's self. 01:28:26 <zzzzzZZZZzzz> in line 5 we name our function 01:28:30 <zzzzzZZZZzzz> main() 01:29:27 <zzzzzZZZZzzz> and give it the "parameters", or "arguments" 'argument1' and 'argument2', these are a bit ambiguously named, because we will re-use this code to explain several more concepts as we go. 01:29:37 <zzzzzZZZZzzz> by giv 01:29:38 <zzzzzZZZZzzz> give 01:30:09 <zzzzzZZZZzzz> I mean we define that the function accepts two named variables as input, named argument1, and argument2 01:31:23 <zzzzzZZZZzzz> In more technical terms, we introduced those two variables to the 'scope' of the function. 01:32:12 <zzzzzZZZZzzz> So what we have here then is a function that just takes two arguments and prints them on the screen. 01:32:26 <zzzzzZZZZzzz> the next important bit being line 13 01:32:32 <zzzzzZZZZzzz> the call 01:33:33 <zzzzzZZZZzzz> In the line we use our main function, or call it from our code (remember python starts at the beginning of the file and works straight down through the outer layer control structures) 01:33:42 <zzzzzZZZZzzz> this* 01:35:04 <zzzzzZZZZzzz> So on this line we have "passed" our main() function two values, string literal "Hello there" and sys.argv[1] (which is the first command line parameter.) 01:35:53 <zzzzzZZZZzzz> without mentioning the arguments name 01:36:08 <zzzzzZZZZzzz> these are called "positional" arguments 01:37:06 <zzzzzZZZZzzz> as opposed to what's the word "optional" arguments? 01:37:15 <zzzzzZZZZzzz> Which I will discuss more in a bit. 01:38:31 <zzzzzZZZZzzz> Anyhow that is how we define a function and give it data to work on, but what if we want to get some data back from the function? 01:40:40 <zzzzzZZZZzzz> http://pastebin.com/3cWXPFsC 01:40:59 <zzzzzZZZZzzz> The return statement. 01:41:29 <zzzzzZZZZzzz> With this we can use our functions, not only to operate on data, but to act in the place of data 01:42:10 <zzzzzZZZZzzz> for instance variableContainingAString = main( "rofl" "mao") 01:43:01 <zzzzzZZZZzzz> is valid python in this instance, and variableContainingAString would equal (== in python) "roflmao" 01:43:31 <zzzzzZZZZzzz> This also lends to demonstrating why functions are useful: re-usability. 01:43:43 * alphis ([email protected]) has joined #CSIII 01:45:20 <zzzzzZZZZzzz> So lets see 01:45:44 <zzzzzZZZZzzz> Ever unprepared, what don't you all now yet... 01:46:00 <hatter> xD 01:46:06 <hatter> objects 01:46:21 <zzzzzZZZZzzz> We also introduced type conversion here, which leads to objects 01:47:38 * rorschach has quit (Ping timeout) 01:47:55 <zzzzzZZZZzzz> the str() function converts an "object" to an object of the type string, it is a python built-in, and is reasonably sane in its implementation,, therfore it is reusable. 01:48:24 * Tsunami ([email protected]) has joined #CSIII 01:48:35 <e> hello 01:48:38 <e> we are talking about python? 01:48:55 <e> str calls __str__() on the object. 01:48:59 <zzzzzZZZZzzz> any construct in python is an object, meaning a variable is an object, a function is an object, a class, a string literal, a whole python module, they are all treated internally as an 'object' in the modern implementation of python. 01:49:07 <zzzzzZZZZzzz> yes, go e 01:49:22 <e> ye everything in python is a PyObject 01:49:44 * rorschach ([email protected]) has joined #CSIII 01:49:47 <e> >>> (15).__str__() 01:49:48 <e> '15' 01:49:56 <zzzzzZZZZzzz> ^\ 01:50:18 <e> >>> str().__str__().__str__().__str__() 01:50:18 <e> '' 01:50:20 <e> ad infinitum 01:50:43 <hatter> e: how i define my own object/module/class thingamajigger? 01:50:48 <zzzzzZZZZzzz> ^ everything is an object, objects have a minimal set of junctionality they are supposed to expose. 01:50:56 <e> class Name(object): ... 01:50:58 <zzzzzZZZZzzz> fnctionality. 01:51:01 <zzzzzZZZZzzz> wtf 01:51:05 <e> new style objects inherit the object object. 01:51:22 <hatter> oh, ok 01:51:26 <moot[GAR]> Easy, this is a beginner lecture. I don't think we need to get into old/new objects 01:51:29 <moot[GAR]> just yet 01:51:38 <hatter> lol I could track it 01:51:39 <zzzzzZZZZzzz> lol 01:51:50 <e> for most cases, just inherit object. 01:51:51 <moot[GAR]> We're covering scope and function definitions 01:51:59 <e> and don't worry about it tbh. 01:52:08 <hatter> all I know is everything between py 2.4 <-> 3.x broke when porting across versions 01:52:17 <hatter> Im guessing that has something to do with 01:52:18 <moot[GAR]> well no fuckin shit 01:52:18 <hatter> old and new 01:52:19 <hatter> :P 01:52:24 <moot[GAR]> 2.4 is like 12 years old 01:52:30 <hatter> Unless you are referring to new() 01:52:38 <e> __new__ 01:52:40 <e> has to do with metacalsses 01:52:46 <moot[GAR]> Yeah, don't mess with __new__ 01:52:46 <hatter> ahhhhh k 01:52:49 <e> u will never need to use them 99.97% of the time 01:52:50 <moot[GAR]> It's dark vodoo 01:52:51 * [moot[GAR]] ([email protected]): moot 01:52:51 * [moot[GAR]] is a registered nick 01:52:51 * [moot[GAR]] #python #CSIII +#wtfux 01:52:51 * [moot[GAR]] allah.irc :BISMILLAH AR RAHMAN AR RAHIM 01:52:51 * [moot[GAR]] idle 00:00:01, signon: Tue Aug 28 15:17:29 01:52:51 * [moot[GAR]] End of WHOIS list. 01:52:55 <moot[GAR]> very dark python magic 01:52:55 <hatter> lol 01:52:56 <zzzzzZZZZzzz> #python. 01:52:58 <moot[GAR]> Yes 01:53:02 <moot[GAR]> Shutting up 01:53:05 <e> ok let zzzzzZZZZzzz go on 01:53:17 <zzzzzZZZZzzz> No, just plugging 01:53:17 <zzzzzZZZZzzz> lol 01:53:23 <zzzzzZZZZzzz> um 01:53:33 <zzzzzZZZZzzz> I was kinda getting lost 01:54:00 <zzzzzZZZZzzz> So 01:54:11 <zzzzzZZZZzzz> Point is if you change 01:54:27 <zzzzzZZZZzzz> print main( "Hello there", sys.argv[1] ) 01:54:38 <zzzzzZZZZzzz> to print main( 1337: , sys.argv[1] ) 01:54:39 <e> zzzzzZZZZzzz: is this py2.x or 3.x? 01:55:22 <zzzzzZZZZzzz> Haven't really crossed that yet, I suppose we'll just say 3, but that means someone has to make sure i don't mix shit up. 01:55:43 <e> stick to 2.x imo 01:55:55 <e> all the good hacker shit is still 2.x compat only 01:56:15 <zzzzzZZZZzzz> tbh, I dunno if we will run into that problem for a bit 01:56:29 <zzzzzZZZZzzz> slow course, atm unless I like, actually write notes 01:56:45 <e> go on with ur lecture, we'll discuss it later in PM 01:56:58 <zzzzzZZZZzzz> truth 01:57:11 <zzzzzZZZZzzz> right ok so 01:57:21 <zzzzzZZZZzzz> <zzzzzZZZZzzz> Point is if you change 01:57:21 <zzzzzZZZZzzz> <zzzzzZZZZzzz> print main( "Hello there", sys.argv[1] ) 01:57:21 <zzzzzZZZZzzz> <zzzzzZZZZzzz> to print main( 1337: , sys.argv[1] ) 01:57:34 <zzzzzZZZZzzz> if we use str() 01:57:43 <e> s/:// 01:57:53 <zzzzzZZZZzzz> honestly nothing really good happens because print is magic 01:57:56 <zzzzzZZZZzzz> but 01:58:33 <Dwaan> e: 01:58:58 <zzzzzZZZZzzz> We convert our integer literal into a string with str() which as e mentioned really just calls .__str__() on the object passed as it's argument. 01:59:08 <zzzzzZZZZzzz> and return the string representation 01:59:17 <zzzzzZZZZzzz> (str() does) 01:59:19 <d4rch0n> fyi: python3 wont allow print with space 01:59:24 <zzzzzZZZZzzz> i c 01:59:28 <d4rch0n> print(blah), not print blah 01:59:35 <moot[GAR]> Let's call this a Python 2 lecture 01:59:44 <moot[GAR]> and proceed appropriately 01:59:44 <zzzzzZZZZzzz> That actually got me last class 01:59:55 <d4rch0n> can always invoke script with python -3 $script 02:00:31 <zzzzzZZZZzzz> TL;DR i don't know the difference but all my code runs anyway, because im 12 and what is this 02:01:09 <foo> ^ this 02:01:16 <zzzzzZZZZzzz> also i think i am cut off?