0

I have a class like this in python:

class xyz:
    def abc():
          #Do Something
            def add(x,y):
                  #Add x and y
            def sub(x,y):
                  #Subtract y from x
            def mul(x,y):
                  #multiply x ynd y

Note: Here "add", "sub" and "mul" are nested methods.

  1. Is it allowed to make a class like this?
  2. If it is allowed, then i want to access the methods "add", "sub" and "mul" by function parameters. for e.g.:

    from xyz import xyz
    def jkl(input):
           abc.(input)  # By doing this it will raise syntax error. This is just for refernece.
    

so, when i call jkl(add(2,3)) it should add the numbers. Similarly when i call jkl(sub(4,3)) it should subtract the numbers.

Can it be done?

sats
  • 153
  • 5
  • 14
  • You can overload standard operators. See here: http://stackoverflow.com/questions/5438574/python-operator-overload-and-friends – Pynchia May 15 '15 at 13:08
  • This doesn't really answer your question, but please use `#` instead of `//` for comments in Python. String literals are good too. – Kevin May 15 '15 at 13:11
  • Just a thought: you could create a dictionary and assign functions to the dictionary values. So that way you can do jkl(key="add", args=(2,3)) which would then call abc[key](args). But there's probably a different approach to whatever it is you're really trying to do that would work better. – Kyle Pittman May 15 '15 at 13:12
  • It might be just me, but I can't follow what you're trying to do. What is your `def abc:` block supposed to do? It just causes a syntax error. – khelwood May 15 '15 at 13:13
  • It looks like OP is attempting to use a class as a module/package. This answer to this question will need to explain how module/package imports work. Either that, or staticmethod. – Rick May 15 '15 at 13:15
  • You aren't using the class `xyz` at all in the example of what you want to do. Please edit your question to show clearly what you want to do, with less abstract names if possible. – chepner May 15 '15 at 13:31
  • Also: as written, your code will raise a `SyntaxError`. You have to have parentheses after `def abc`, viz. `def abc():`. – Rick May 15 '15 at 13:33

1 Answers1

1

It's hard to understand exactly what your goal is here.

EDIT: I'll leave this answer here since it has received an upvote, but OP clarified that they're actually asking how to overload operators.

You can do this:

class abc():
      //Do Something
      @staticmethod
      def add(x,y):
              # Add x and y
      @staticmethod
      def sub(x,y):
              # Subtract y from x
      @staticmethod
      def mul(x,y):
              # multiply x ynd y

abc.add(1,1)

If you want to use your class in another module, you can put that class definition in file xyz.py, and then in another file (in the same directory):

from xyz import abc

abc.add(1,1)

However, this is a very odd thing to do in the Python world. I'd advise organizing things differently, unless you have a really good reason to do things this way. A better way would be to skip the class definition altogether, and do this in abc.py:

def add(x,y):
          # Add x and y
def sub(x,y):
          # Subtract y from x
def mul(x,y):
          # multiply x ynd y

Then import from that module:

import abc

abc.add(1,1)
etc...

Alternatively, you can do this:

from abc import add, sub, mul

add(1,1)
etc...
Rick
  • 43,029
  • 15
  • 76
  • 119
  • Sorry for the confusion. I just need to overload standard operators. Thnx. – sats May 15 '15 at 13:44
  • @sats I'd suggest completely rewriting your question before it gets closed, then! Or just starting over. – Rick May 15 '15 at 13:45