Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return Multiple or one just
#1
I can import two classes (nothing abnormal), but the problem is that I need to call the method of each class using only one return or more, I tried the instructions below, but without success:

- Exemple One

from apps.document.views import classone, classtwo

class Runcode:

    def screen():
        # exemple one : 
        return classone.ClassOne.view(), classone.ClassOne.view()
- Exemple Two

from apps.document.views import classone, classtwo

class Runcode:

    def screen():
        # exemple two : 
        return classone.ClassOne.view()
        return classtwo.ClassTwo.view()
Reply
#2
Does the first try not work because you are getting two classOne's?

This works fine for me:
class X:
    def letter(self):
        return 'X'

class Y:
    def letter(self):
        return 'Y'

class Z:
    def duals(self):
        return X().letter(), Y().letter()

print(Z().duals())
Reply
#3
(Apr-29-2020, 07:56 PM)deanhystad Wrote: Does the first try not work because you are getting two classOne's?

This works fine for me:
class X:
    def letter(self):
        return 'X'

class Y:
    def letter(self):
        return 'Y'

class Z:
    def duals(self):
        return X().letter(), Y().letter()

print(Z().duals())

Okay, it was just a matter of type but they are different classes (classeone and classetwo).

Thank you for the example shown above, I made the correction and I need the solution to be in accordance with the writing of the two examples above. In the first example I get the following error?

from apps.document.views import header, footer

class Runcode:

    def screen():

        return header.Header.view(), footer.Footer.view()
Output I get: ('\n\n\n \n\n \n \n \n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n \n \n \n\n \n \n ', '\n \n \n \n \n\n ')
Reply
#4
as usual in your threads you provide insufficient/partial information. The problem is what is returned by both class methods view() which we don't know anything about. the output you get is exactly as expected - a tuple of 2 elements, strings.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Apr-30-2020, 09:48 AM)buran Wrote: as usual in your threads you provide insufficient/partial information. The problem is what is returned by both class methods view() which we don't know anything about. the output you get is exactly as expected - a tuple of 2 elements, strings.

Hello Buran,
As you can see he is calling the same method but from different classes. In each class with its view () methods there is an attribute that stores pure html code, which is triggered on the return of each view method, in isolation.
Reply
#6
as far as I understand - header.Header.view() returns '\n\n\n \n\n \n \n \n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n \n \n \n\n \n \n ' and footer.Footer.view() returns '\n \n \n \n \n\n '. You expect some html, but actually you get that. why - we cannot say, because we haven't seen your classes.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
So is it working or not? You are getting a tuple holding the return values for the two calls. Is that success?
Reply
#8
(Apr-30-2020, 01:00 PM)buran Wrote: as far as I understand - header.Header.view() returns '\n\n\n \n\n \n \n \n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n \n \n \n\n \n \n ' and footer.Footer.view() returns '\n \n \n \n \n\n '. You expect some html, but actually you get that. why - we cannot say, because we haven't seen your classes.

let's consider this code below, initially:

from apps.document.views import header, footer

class Runcode:

    def screen():
        v_html = (
            header.Header.view(),
            footer.Footer.view()
        )
        return v_html
Class Header :

# Class Header : modelo de cabeƧalho html.
class Header:
    # class-method : view
    def view():
        # class-attribute :
        v_html5 = """
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>

        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>

        <!--Import Google Icon -->
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

        <!--Import Material Design Icons-->
        <link href="https://cdn.materialdesignicons.com/2.8.94/css/materialdesignicons.min.css" rel="stylesheet">

        <!--Import Google Font -->
        <link href="https://fonts.googleapis.com/css?family=Catamaran&display=swap" rel="stylesheet">

        <!-- Favicon -->
        <link  href="" rel="shortcut icon"/>

        <!--Import framework bootstrap-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

        <!--Custom framework design-->
        <link href="http://127.0.0.1:8080/document/assets/css/custom.css" rel="stylesheet" type="text/css">
        
        <!-- Page Title -->
        <title>document</title>

    </head>
    <body>
        """
        return v_html5
Class Footer :
# Class Header : modelo de cabeƧalho html.
class Footer:
    # class-method :
    def view():
        # class-attribute :
        v_html5 = """
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
</html>
        """
        return v_html5
Erro : ('\n\n\n \n\n \n \n \n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n\n \n \n \n \n \n\n \n \n ', '\n \n \n \n \n\n ')
Reply
#9
Header and Footer are not classes. You use the word class, but you do not use any of the class coding conventions.
Reply
#10
again, you present some hypothetical code, claiming it's not working. However, despite all problems, and there are huge problems with your code, it's working

class Header:
    # class-method : view
    def view():
        # class-attribute :
        v_html5 = """
<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
 
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
        <meta name="description" content=""/>
        <meta name="keywords" content=""/>
 
        <!--Import Google Icon -->
        <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
 
        <!--Import Material Design Icons-->
        <link href="https://cdn.materialdesignicons.com/2.8.94/css/materialdesignicons.min.css" rel="stylesheet">
 
        <!--Import Google Font -->
        <link href="https://fonts.googleapis.com/css?family=Catamaran&display=swap" rel="stylesheet">
 
        <!-- Favicon -->
        <link  href="" rel="shortcut icon"/>
 
        <!--Import framework bootstrap-->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
 
        <!--Custom framework design-->
        <link href="http://127.0.0.1:8080/document/assets/css/custom.css" rel="stylesheet" type="text/css">
         
        <!-- Page Title -->
        <title>document</title>
 
    </head>
    <body>
        """
        return v_html5


class Footer:
    # class-method :
    def view():
        # class-attribute :
        v_html5 = """
        <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
        <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    </body>
</html>
        """
        return v_html5


class Runcode:
 
    def screen():
        v_html = (
            Header.view(),
            Footer.view()
        )
        return v_html

print(Runcode.screen())
Output:
('\n<!DOCTYPE html>\n<html lang="en" dir="ltr">\n <head>\n \n <meta charset="utf-8">\n <meta http-equiv="X-UA-Compatible" content="ie=edge">\n <meta name="viewport" content="width=device-width, initial-scale=1.0"/>\n <meta name="description" content=""/>\n <meta name="keywords" content=""/>\n \n <!--Import Google Icon -->\n <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">\n \n <!--Import Material Design Icons-->\n <link href="https://cdn.materialdesignicons.com/2.8.94/css/materialdesignicons.min.css" rel="stylesheet">\n \n <!--Import Google Font -->\n <link href="https://fonts.googleapis.com/css?family=Catamaran&display=swap" rel="stylesheet">\n \n <!-- Favicon -->\n <link href="" rel="shortcut icon"/>\n \n <!--Import framework bootstrap-->\n <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">\n \n <!--Custom framework design-->\n <link href="http://127.0.0.1:8080/document/assets/css/custom.css" rel="stylesheet" type="text/css">\n \n <!-- Page Title -->\n <title>document</title>\n \n </head>\n <body>\n ', '\n <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>\n <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>\n <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>\n </body>\n</html>\n ')
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Function - Return multiple values tester_V 10 4,439 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Return Object Created from Multiple Classes emerger 3 3,054 Oct-18-2018, 02:14 AM
Last Post: emerger
  Multiple "return" statements or "result" variable? WolfWayfarer 7 7,747 Jul-25-2018, 07:51 AM
Last Post: perfringo
  How to return multiple values from function in python pikkip 2 4,611 Jan-23-2017, 06:00 AM
Last Post: pikkip

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020