Why Use Clases?


#1


Maybe a stupid question but, how do I write the below code in python 3.5.2 idle shell?

The problem is functions (def) are always 4 spaces indented below class and there is one row space between def functions. If I try to do the same in idle it jumps to a new line (after the first def) and even if I start the new function with 4 space indent it is no longer recognised as part of class function sequence!

please help me!


class Fruit(object):
    """A class that makes various tasty fruits."""
    def __init__(self, name, color, flavor, poisonous):
        self.name = name
        self.color = color
        self.flavor = flavor
        self.poisonous = poisonous

    def description(self):
        print "I'm a %s %s and I taste %s." % (self.color, self.name, self.flavor)

    def is_edible(self):
        if not self.poisonous:
            print "Yep! I'm edible."
        else:
            print "Don't eat me! I am super poisonous."

lemon = Fruit("lemon", "yellow", "sour", False)

lemon.description()
lemon.is_edible()


#2

Paste the code into a new file, save it and then Run (F5). Or you can paste it directly into the shell, but it won't be saved, only in memory where you can run it.

The code is directly runnable with one minor change:

print "something"

becomes,

print ("something")

since print is now a function. The old string formatting method still works, but there is an improved function, format() that has a little more horsepower.

print("I'm a {1:s} {2:s} and I taste {3:s}.".format(self.color,self.name,self.flavor))

The numbers are optional if the order is the same as the parameter list. So too is the type specifier given the arguments are all strings. If we wish to print in a different order, then switch the numbers around.

https://pyformat.info/


#3

Thanks, any suggestions as to how I can write it from scratch rather than copy pasting it?

I particularly do not understand how the one row space is made between functions init /description and is_edible.


#4

On your own machine? Just open a new file and start typing.

Just enter an extra line break (if I get your meaning).


#5

I mean the space below. In idle if I push enter button to make the space below it jumps to a new line which has the same indentation as the class.

Hope I managed to explain clearly.

class Fruit(object):
    """A class that makes various tasty fruits."""
    def __init__(self, name, color, flavor, poisonous):
        self.name = name
        self.color = color
        self.flavor = flavor
        self.poisonous = poisonous
I MEAN THIS ONE ROW SPACE.
    def description(self):

#6

If you hit Enter twice, then backspace to the left margin and type four spaces, then begin the next method header.


#7

If I hit enter twice it jumps to a new line?


#8

Are you referring to the editor? Or the interactive console?


#9

I refer to idle shell.


#10

You mean this...

>>>

?


#11

yep exactly, thats it


#12

You are far better off to open a new file and write your code in the editor. Typing class code in the shell is much less convenient.

In the File menu of the shell, click New File. Then code away and save it in your project folder. F5 or Run from the Run menu.

I haven't tried it, but what happens if you hold the control key and press Enter (in the shell)?


#13

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.