Calling a Function Within a Class Method

How can I call a function within a class?

Since these are member functions, call it as a member function on the instance, self.

def isNear(self, p):
self.distToPoint(p)
...

calling a method inside a class-Python

You haven't created an object to the above class.

Any function/method inside a class can only be accessed by an object of that class .For more information on the fundamentals of Object Oriented Programming, please check this page.

Meanwhile for this to work, define your class in the following way :

class Time:

def __init__(self,x=None,y=None,z=None):
self.hour=x
self.minute=y
self.second=z

def __str__(self):
return "({:02d}:{:02d}:{:02d})".format(self.hour, self.minute, self.second)

def time_to_int(time):
minutes=time.hour*60+time.minute
seconds=minutes*60+time.second
return seconds

def int_to_time(seconds):
time=Time()
minutes,time.second=divmod(seconds,60)
time.hour,time.minute=divmod(minutes,60)
return time

def add_time(t1,t2):
seconds=time_to_int(t1)+time_to_int(t2)
return int_to_time(seconds)

and outside the class block, write the following lines :

TimeObject = Time()
start=Time(9,45,00)
running=Time(1,35,00)
TimeObject.add_time(start,running)
print "done"

I however suggest you to write the add_time function outside the class because you are passing the objects to the class as the parameters to the function within the same class and it is considered as a bad design in object oriented programming.
Hope it helps. Cheers!

Defining and Calling a Function within a Python Class

Define and call plus_2_times_4 with self, namely:

class ExampleClass():

def __init__(self, number):
self.number = number

def plus_2_times_4(self,x):
return(4*(x + 2))

def arithmetic(self):
return(self.plus_2_times_4(self.number))

This will work.

Calling one method from another within same class in Python

To call the method, you need to qualify function with self.. In addition to that, if you want to pass a filename, add a filename parameter (or other name you want).

class MyHandler(FileSystemEventHandler):

def on_any_event(self, event):
srcpath = event.src_path
print (srcpath, 'has been ',event.event_type)
print (datetime.datetime.now())
filename = srcpath[12:]
self.dropbox_fn(filename) # <----

def dropbox_fn(self, filename): # <-----
print('In dropbox_fn:', filename)

Call a function inside of a function inside of a class in Python

Updated after question edit:

Check out this link that shows how to make a "closure" https://stackoverflow.com/a/4831750/2459730

It's what you described as a function inside of a function.

def run(self):
def calculate(self): # <------ Need to declare the function before calling
return self.number**2

print "I will now square your number"
print "Your number squared is: "
print self.calculate() # <---- Call after the function is declared

Before question edit:
Your calculate function isn't indented properly.

def run(self):
print "I will now square your number"
print "Your number squared is: "
print self.calculate()

#This squares the number
def calculate(self): # <----- Proper indentation
return self.number**2 # <------ Proper indentation

The calculate function should have the same indentation level as the run function.

Is it un-pythonic to define a function inside of a class method?

Creating a nested function for a callback is just fine. It even gives that function access to any locals in the parent function (as closures).

You can use a lambda if all you need to execute is one expression:

class TestClass(ParentClass):
def __init__(self):
ParentClass.map_event_to_callback(ParentClass.event, lambda text: print("hello"))

Calling a class function inside of __init__

Call the function in this way:

self.parse_file()

You also need to define your parse_file() function like this:

def parse_file(self):

The parse_file method has to be bound to an object upon calling it (because it's not a static method). This is done by calling the function on an instance of the object, in your case the instance is self.



Related Topics



Leave a reply



Submit