-3

So I am confident working with functions now, and passing values in.

So for example

def myfunction(arg1,arg2,arg3):
    print 'my args %s %s %s' % (arg1, arg2, arg3)

I can then call that function as follows

myfunction(arg1,arg2,arg3)

What I would like to do is move myfunction, into MyClass:

MyClass():
    myfunction(arg1,arg2,arg3)

I have no idea how to pass these args (variables) in or call them from outside, as all of the examples online don't seem to match this case.

Sorry if this is very rudimentary / basic , but i am struggling to jump the gap here in my understanding.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
decodebytes
  • 411
  • 4
  • 16
  • `object.myfunction(x, y, z)` – Barmar Dec 11 '15 at 21:01
  • You call as `MyClass.myfunction` (along with adding staticmethod decorator) if this has no references to the current instance. Or if it does then: inside the class `myfunction(arg1,arg2,arg3)` should be `myfunction(self,arg1,arg2,arg3)` because you need to pass the class parameter. Then call by creating an instance and using that method. – shuttle87 Dec 11 '15 at 21:02

3 Answers3

3

You need to define your class and then function as a method. If the method is not static then you will need to create an instance of the class before you can use the method. For example:

class MyClass(object):
    def myfunction(self, arg1, arg2, arg3):
        print 'my args %s %s %s' % (arg1, arg2, arg3)

# usage
my_class_instance = MyClass()
my_class_instance.myfunction('1', '2', '3')

By the way, I find it hard to believe that you couldn't find any relevant information as this is the very basic usage of classes.

DeepSpace
  • 78,697
  • 11
  • 109
  • 154
1

I'd suggest you start by reading the tutorial on classes: https://docs.python.org/2/tutorial/classes.html When you define a method in a class Python will pass the current instance as the first parameter unless you tell it not to. The convention is to call this first parameter self. Because your function has nothing to do with the current instance it looks like you need a staticmethod:

MyClass():
    @staticmethod
    myfunction(arg1,arg2,arg3):
       print 'my args %s' % (arg1, arg2, arg3)

Note there is no self parameter here because this is a staticmethod. You then call like:

MyClass.myfunction("a","b","c")

Read this question for more information: What is the difference between @staticmethod and @classmethod in Python?

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106
1

Classes are not simply a wrapper for functions, they provide a new level of abstraction. I highly suggest you read the documention https://docs.python.org/2/tutorial/classes.html and read about object oriented programming in general.

First your class setup is incorrect:

class MyClass():
    def __init__(self):
        #classes usually want to have a constructor
        pass

Secondly every function in your class needs to have self as the first parameter:

def myfunction(self, arg1, arg2, arg3):
    #functionality here

Then you have to create an instance of the class

instance = MyClass()
#instance will be of type MyClass.

You can now call the function on that instance:

instance.myfunction('1','2','3')
Philip Feldmann
  • 7,887
  • 6
  • 41
  • 65