Why my function returns nothing?

This is the place for queries that don't fit in any of the other categories.

Why my function returns nothing?

Postby leoChristofoli » Tue Aug 13, 2013 3:50 pm

Hi all,

I know its my first post and I already asking for help, but I promise I'll help you in everything that I can. So please

This is the problem.

I'm starting at python's coding and I did some simple programs using pyDev. Like this:

Code: Select all
class bag:
    def __init__ (self, oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe):
        self.oldPath = oldPath
        self.newPath = newPath
        self.oldLines = oldLines
        self.newLines = newLines
        self.oldLset = oldLset
        self.newLset = newLset
        self.oldAdd = oldAdd
        self.oldRe = oldRe

    def text (self, oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe):
   
        for line in oldLines:
            if line in oldAdd:
                print '-',line.strip()
            elif line in oldRe:
                print '+',line.strip()
                 
        for line in newLines:
            if line in oldAdd:
                print '-', line.strip()
            elif line in oldRe:
                print '+', line.strip()
           
oldPath = 'original.txt'
newPath = 'new.txt'
       
oldLines = file(oldPath).read().split('\n')
newLines = file(newPath).read().split('\n')
       
oldLset = set(oldLines)
newLset = set(newLines)
       
oldAdd = oldLset - newLset
oldRe = newLset - oldLset


But it returns nothing, neither an error nor a print.

And the odd is that I tested a simple function, and it had the same problem:

Code: Select all
def soma(a,b):
    return a+b

soma(1,2)


I tried ninja IDE, but the same problem.
It runs on python's IDLE, but I can't execute the .py file.

I'm using:
Win7
python 2.7 and tested 3 too
eclipse with pyDev plugin
ninja IDE
leoChristofoli
 
Posts: 9
Joined: Tue Aug 13, 2013 2:51 pm

Re: Why my function returns nothing?

Postby Yoriz » Tue Aug 13, 2013 4:02 pm

Hi, welcome to the forum.
To see the value returned when using an ide you will have to print it.
Code: Select all
def soma(a,b):
    return a+b

print soma(1,2)

Add () to the print if using python 3.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Why my function returns nothing?

Postby leoChristofoli » Tue Aug 13, 2013 4:39 pm

Yoriz wrote:Hi, welcome to the forum.
To see the value returned when using an ide you will have to print it.
Code: Select all
def soma(a,b):
    return a+b

print soma(1,2)

Add () to the print if using python 3.

Ow man, thanks, my mistake. :oops:

But and on the first code, class bag. Why it does not print the text function?

If I put it like this it work well. But I can't run it using the class.

Code: Select all
def text (oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe):
   
    for line in oldLines:
        if line in oldAdd:
            print '-',line.strip()
        elif line in oldRe:
            print '+',line.strip()
                 
    for line in newLines:
        if line in oldAdd:
            print '-', line.strip()
        elif line in oldRe:
            print '+', line.strip()
           

oldPath = 'original.txt'
newPath = 'new.txt'
       
oldLines = file(oldPath).read().split('\n')
newLines = file(newPath).read().split('\n')
       
oldLset = set(oldLines)
newLset = set(newLines)
       
oldAdd = oldLset - newLset
oldRe = newLset - oldLset   
text(oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe)


thank you
leoChristofoli
 
Posts: 9
Joined: Tue Aug 13, 2013 2:51 pm

Re: Why my function returns nothing?

Postby Yoriz » Tue Aug 13, 2013 4:47 pm

You have defined the class bag but have not made an instance of bag and not called its text method.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 789
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Why my function returns nothing?

Postby leoChristofoli » Wed Aug 14, 2013 12:34 pm

Yoriz wrote:You have defined the class bag but have not made an instance of bag and not called its text method.

Man, I tried to do this way:
Code: Select all
class bang:
    def __init__ (self, oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe):
        self.oldPath = oldPath
        self.newPath = newPath
        self.oldLines = oldLines
        self.newLines = newLines
        self.oldLset = oldLset
        self.newLset = newLset
        self.oldAdd = oldAdd
        self.oldRe = oldRe
       
    def text (self, oldPath, newPath, oldLines, newLines, oldLset, newLset, oldAdd, oldRe):
                   
        x = bang()
           
        for line in oldLines:
            if line in oldAdd:
                print '-',line.strip()
            elif line in oldRe:
                print '+',line.strip()
                 
        for line in newLines:
            if line in oldAdd:
                print '-', line.strip()
            elif line in oldRe:
                print '+', line.strip()


but now I don't know how to use it. How do I make this instance to use it on another class?
I've read python's documentation but it didn't help me.
leoChristofoli
 
Posts: 9
Joined: Tue Aug 13, 2013 2:51 pm

Re: Why my function returns nothing?

Postby Marbelous » Thu Aug 15, 2013 8:26 pm

You're not using classes correctly. You create a class with the class keyword and define attributes and methods in it. Then, OUTSIDE the class definition, you call the class to make instances of it. Then, you can call the methods through the instances. This is the one of the main points of OOP, you may want to study the basic concepts a little more before you try to use them in Python.

Code: Select all
class Klass():

    data1 = 'Ni!'  # Two attributes
    data2 = 'spam'

    def __init__(self, name = 'DeFau', value = 0):  # The Constructor
        self.name = name
        self.data = value

    def display(self):  #  A Display Methed
        print 'My name is %s, and my value is %d.' % (self.name, self.data)


i1 = Klass()  # A Default instantiation
i2 = Klass('Tim', 123)  # Another instatiation

i1.display()  #Calling the Display method in the instances
i2.display()
Marbelous
 
Posts: 138
Joined: Fri May 31, 2013 8:12 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests