Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple functions in Python

We have some variable, or other instance: a='?'. We have such input:

f = a(3112).bas(443).ssad(34) 

When we type

print(f)

Output should be:

3112a-443bas-34ssad

I've tried some ways to solve this and have found information about chaining, but I still have the problem. I can't return class name to the brginning of the string.

This, what I have:

class A():       

    def __getattribute__(self, item):
        print (str(item))
        return super(A, self).__getattribute__(item)    

    def __init__(self, x):
        self.x = x
        print (str(x))

    def b(self, item):
        print (str(item))        
        return self

    def c(self, item):
        print (str(item))
        return self

    def d(self, item):
        print (str(item))
        return self

A(100).b(200).c(300).d(400)

My output:

100
b
200
c
300
d
400

But I couldn't concatenate it in one string.

like image 782
Maksym Yankin Avatar asked Feb 16 '18 09:02

Maksym Yankin


People also ask

What are the 4 types of functions in Python?

Types Of Python FunctionsPython Built-in Functions. Python Recursion Functions. Python Lambda Functions.

Can we define two functions in Python?

There are two basic types of functions: built-in functions and user defined functions. The built-in functions are part of the Python language; for instance dir , len , or abs . The user defined functions are functions created with the def keyword.

What are the 3 different function in Python?

There are three functions in python that provide vast practicality and usefulness when programming. These three functions, which provide a functional programming style within the object-oriented python language, are the map(), filter(), and reduce() functions.


3 Answers

Dynamic way

class A(object):

    def __init__(self, integer):
        self._strings = ['{}a'.format(integer)]


    def __getattr__(self, attrname, *args):
        def wrapper(*args, **kwargs):
            self._strings.append('{}{}'.format(args[0], attrname))
            return self

        return wrapper


    def __str__(self):
        return '-'.join(self._strings)


print(A(100).bas(200).ssad(300))

Output

100a-200bas-300ssad

But also

 print(A(100).egg(200).bacon(300).SPAM(1000))

Output

100a-200egg-300bacon-1000SPAM

Static way

class A(object):

    def __init__(self, integer):
        self._strings = ['{}a'.format(integer)]


    def bas(self, integer):
        self._strings.append('{}bas'.format(integer))
        return self

    def ssad(self, integer):
        self._strings.append('{}ssad'.format(integer))
        return self

    def __str__(self):
        return '-'.join(self._strings)


print(A(100).b(200).c(300))

Output

100a-200bas-300ssad

More about __str__

like image 70
Arount Avatar answered Oct 22 '22 08:10

Arount


You can override the __str__ method to define your specific output:

class A():
    def __init__(self, a, b="", c="", d=""):
        self._a = a
        self._b = b
        self._c = c
        self._d = d

    def __str__(self):
        return '{}a-{}b-{}c-{}d'.format( self.a, self.b, self.c, self.d )

    def b(self, item):
        self._b = item
        return self

    def c(self, item):
        self._c = item
        return self

    def d(self, item):
        self._d = item
        return self

f = A(100).b(200).c(300).d(400)
print(f)  # 100a-200b-300c-400d
like image 4
Skandix Avatar answered Oct 22 '22 08:10

Skandix


Here I tried it in another way , ie, If you want to take the function name instead of manually giving it you can use inspect in python. Try this code :

import inspect


class A():
   l = []

   def __init__(self, x):
        self.x = x
        print (str(x))
        self.l.append(str(x) + "a")

    def b(self, item):
        print (str(item))
        self.l.append(str(item) + inspect.stack()[0][3])
        return self

    def c(self, item):
        print (str(item))
        self.l.append(str(item) + inspect.stack()[0][3])
        return self

    def d(self, item):
        print (str(item))
        self.l.append(str(item) + inspect.stack()[0][3])
        return self


print("-".join(A(100).b(200).c(300).d(400).l))

The o/p is like :

'100a-200b-300c-400d'
like image 2
Vikas Periyadath Avatar answered Oct 22 '22 08:10

Vikas Periyadath