Help with method(s)

Questions about programming languages and debugging
Post Reply
User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

Help with method(s)

Post by Swan »

Ok...I am STILL having a bit of trouble with methods...not 100% sure about them.

Code: Select all

def restrict(html, starting_regexp, stopping_regexp)
start = html.index(starting_regexp)
stop = html.index(stopping_regexp, start)
html[start..stop]
end
The above is an example of method (and Im esp. having trouble getting my head around.)

Ok, what im gonna do is write everything i THINK i know about methods and then someone could please correct me?

methods=a function for an object, has the syntax of the codeword (in this case def) followed by the method name, and anything in brackets is the arguments.

in the above code start is a variable assigned the value of html, and the .index is another method on it and the value of html.index is equal to the value of the stuff in the brackets. Ditto for stop.

Ok i have other questions, but they will depend on how right/wrong I am currently with my "interpetation" so far. as i said, not 100%...so any confirmation greatly appreciated.

User avatar
maboroshi
Dr. Mab
Dr. Mab
Posts: 1624
Joined: 28 Aug 2005, 16:00
18

nod

Post by maboroshi »

You have more of an understanding than you think man

although in python html would be a class and index inherits of that class basically a function within that class

unless you had a function with in a function

like

Code: Select all

 def function1
          def function
then the dot notation syntax would be incorrect

if you were calling two seperate functions it would look like this function_1(function_2(<args>))

function 2 is passed as an argument to function 1

anyway cheers

Maboroshi

User avatar
Swan
Knight of the Sword
Knight of the Sword
Posts: 827
Joined: 18 Oct 2006, 16:00
17
Contact:

Post by Swan »

just so it clears things up, my original code is a method from ruby.

The bit that is confusing me is, start and stop, how can they have paramters (starting_regexp) (stopping_regexp, start) respectivelym when it seems they havent even been defined? :-/

Or does it not matter....because the method "sets the foundation anyway?"

G-Brain
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 467
Joined: 08 Nov 2007, 17:00
16
Location: NL

Post by G-Brain »

They are defined, here:

Code: Select all

def restrict(html, starting_regexp, stopping_regexp)
So when you call

Code: Select all

restrict(a,b,c)
The variables html, starting_regexp and stopping_regexp are set accordingly:

Code: Select all

html = a
starting_regexp = b
stopping_regexp = c
Not that I understand what the code you posted accomplishes or anything...

Post Reply