Hmm.....

B

blackneos940

Guest
Let me see if I have this right.....
Code:
class Tuna:

    def __init__(self):

        print ("Random")

    def swim(self):

flipper = Tuna()

flipper.swim()

I can only call swim, if I first call the Tuna class..... Right......? Ugh........ Unbelievable..... A year later, and I still have yet to move beyond __init__..... :( Even with my medication, I'm getting depressed..... But.... That stupid flame keeps me going, despite my ineptitude.......
 


Yes, you've pretty much got it!

There are a couple of things that look a little fishy though, heh heh! You might want to put some code under the Tuna classes swim member-function/method so you can see that it works!

Also after declaring your class, you should perform a check to see whether this module is the main file and add some code to test your module.

Take a look at this:
Code:
#!/usr/bin/python

class Tuna:
    def __init__(self):
        print("Initialising Tuna object")
        # perform any other initialisation here

    def swim(self):
        print("Tuna is swimming!")

# If this is the main file, create a Tuna object and make it swim
if __name__ == '__main__':
    flipper = Tuna();
    flipper.swim();

Now, assuming the file is called Tuna.py, and that it is executable; try running the script like this:
Code:
./Tuna.py
Would cause the test code (under the if statement) to be executed because Tuna.py is the __main__ script that we are running.

You will also be able to import your Tuna class in other scripts without firing off the test code!
So for example, if you want to include your Tuna class in another script you can do this:
e.g.
LakeFullOfTuna.py
Code:
#!/usr/bin/python

from Tuna import Tuna

class LakeFullOfTuna:
    def __init__(self):
        #....
     
# Rest of class definition........
#....
# Then somewhere in the code:
    flopper = Tuna()
    flopper.swim()

Because you have imported the Tuna module into LakeFullOfTuna.py, you can now create instances of the Tuna class in your LakeFullOfTuna script!
Also, because the Tuna class was imported, and therefore not the __main__ file, the test code under the if statement in Tuna.py does not get executed!

Hope I've explained that clearly! :)
 
Last edited:
I just want to make a few things clear in case they are not. To understand the lingo:
swim() is a method. This is a block of code that performs some action and returns a value and is inside a class. A function is like a method but is not located in a class. As was already mentioned Tuna is a class. Classes are templates for the creation of Objects. Remember that Objects are instances of classes. Think about this: If you have 100 classes but never initialize them then no memory is used.

How they work:
To instantiate an Object you must call the class's constructor. A constructor sets up the object with parameters. __init__ is the constructor for the Tuna class. When you instantiate a class by calling
variable = Class()
you are creating the object in memory. To access a method inside a class you have to call it with the member accessor ('.').


Just want to make it all clear. For anyone who sees this. ;)

Example:
https://github.com/ryanvade/robotproject/blob/master/Base.py
 
Last edited:
Yes, you've pretty much got it!

There are a couple of things that look a little fishy though, heh heh! You might want to put some code under the swim function so you can see that it works!

Also after declaring your class, you should perform a check to see whether this module is the main file and add some code to test your module.

Take a look at this:
Code:
#!/usr/bin/python

class Tuna:
    def __init__(self):
        print("Initialising Tuna object")
        # perform any other initialisation here

    def swim(self):
        print("Tuna is swimming!")

# If this is the main file, create a Tuna object and make it swim
if __name__ == '__main__':
    flipper = Tuna();
    flipper.swim();

Now, assuming the file is called Tuna.py, and that it is executable; try running the script like this:
Code:
./Tuna.py
Would cause the test code (under the if statement) to be executed because Tuna.py is the __main__ script that we are running.

You will also be able to import your Tuna class in other scripts without firing off the test code!
So for example, if you want to include your script in another script you can do this:
e.g.
LakeFullOfTuna.py
Code:
#!/usr/bin/python

from Tuna import Tuna

class LakeFullOfTuna:
    def __init__(self):
        #....
     
# Rest of class definition........
#....
# Then somewhere in the code:
    flopper = Tuna()
    flopper.swim()

Because you have imported the Tuna module into LakeFullOfTuna.py, you can now create instances of the Tuna class in your LakeFullOfTuna script!
Also, because the Tuna class was imported, and therefore not the __main__ file, the test code under the if statement in Tuna.py does not get executed!

Hope I've explained that clearly! :)

Heheh..... :D "Fishy" doesn't even BEGIN to DESCRIBE this nautical Program, and it's a favorite App of Sea Sponges and Penguins alike!..... :3 Also I like it!..... :D Especially the way you rendered the Print Functions..... :D I'll copy and paste the altered code into Kate or somethin', and call it LakeFullOfTuna.py..... :3 How's that sound, my Programmin', Drummin', buddy.....? :>
 
I just want to make a few things clear in case they are not. To understand the lingo:
swim() is a method. This is a block of code that performs some action and returns a value and is inside a class. A function is like a method but is not located in a class. As was already mentioned Tuna is a class. Classes are templates for the creation of Objects. Remember that Objects are instances of classes. Think about this: If you have 100 classes but never initialize them then no memory is used.

How they work:
To instantiate an Object you must call the class's constructor. A constructor sets up the object with parameters. __init__ is the constructor for the Tuna class. When you instantiate a class by calling
variable = Class()
you are creating the object in memory. To access a method inside a class you have to call it with the member accessor ('.').


Just want to make it all clear. For anyone who sees this. ;)

Example:
https://github.com/ryanvade/robotproject/blob/master/Base.py

Thank you for that, Darth Ryan..... :3 I SORT of knew some of that stuff, but a second or even 80th look can't hurt..... (it even makes it more CLEAR..... :D) I think the hardest part is the nuances of Programming..... But.... I'm learning..... Slowly, I'm..... getting there..... :) Thank you, I mean it..... :D I hope this doesn't sound weird, I really Love you guys..... :')
 
I just want to make a few things clear in case they are not. To understand the lingo:
swim() is a method. This is a block of code that performs some action and returns a value and is inside a class. A function is like a method but is not located in a class. As was already mentioned Tuna is a class. Classes are templates for the creation of Objects. Remember that Objects are instances of classes. Think about this: If you have 100 classes but never initialize them then no memory is used.

How they work:
To instantiate an Object you must call the class's constructor. A constructor sets up the object with parameters. __init__ is the constructor for the Tuna class. When you instantiate a class by calling
variable = Class()
you are creating the object in memory. To access a method inside a class you have to call it with the member accessor ('.').


Just want to make it all clear. For anyone who sees this. ;)

Example:
https://github.com/ryanvade/robotproject/blob/master/Base.py

Good post Ryan! I didn't explain any of the OO aspects in my post. I assumed that most of those things were already vaguely familiar to blackneos940. But the explanations will help neos and anybody else who is new to Python who reads the thread.

Also I have a habit of using the term 'member-function' rather than 'method', but the two terms are equivalent and interchangeable!

In speech, I often shorten member-function to function. And sometimes that also slips out in my posts. Like in my previous post when I said: "You might want to put some code under the swim function" - Member-function was implied, but that wasn't clear in any way! My bad! :)

swim is a function that is a member of the Tuna class, hence a member function, or a method! ;)

Just noticed a couple of other minor mistakes in my previous post, so I'll fix that line!
 
Good post Ryan! I didn't explain any of the OO aspects in my post. I assumed that most of those things were already vaguely familiar to blackneos940. But the explanations will help neos and anybody else who is new to Python who reads the thread.

Also I have a habit of using the term 'member-function' rather than 'method', but the two terms are equivalent and interchangeable!

In speech, I often shorten member-function to function. And sometimes that also slips out in my posts. Like in my previous post when I said: "You might want to put some code under the swim function" - Member-function was implied, but that wasn't clear in any way! My bad! :)

swim is a function that is a member of the Tuna class, hence a member function, or a method! ;)

Just noticed a couple of other minor mistakes in my previous post, so I'll fix that line!
I say method because of my java background:D. I understood what you meant though. ;)
 
Programming, in the most simplistic terms, is nothing more than methods and variables. In a Class, there are two things - properties and methods (or functions- but functions are methods as are routines in some other programming languages) (routines do not return a value, but functions do, I don't know what the term is in python for routine). Properties are the nouns (variables) (such as height, weight, color) and methods are verbs or actions (methods)(such as swim, stop, sleep).

Classes, once you understand them, are very simple. They're just blueprints for an object that you use in your code. In OOP, everything is an object. Just like in Linux, everything is a file (which is in and of itself, an object), everything in OOP is an object. Once you wrap your head around that, you'll be on your way to writing a lot of great applications that can make other's life easier. It's what I love about programming; I get to make someone's job that much easier to accomplish by automating mundane tasks. How awesome is that?!
 
Last edited:

Members online


Top