Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class init and arguments [duplicate]

Tags:

python

init

I know that this is super basic Python stuff, but the concept doesn't get into my mind. I miss the fundamental reason and the structure to instantate an object under __init__()

This is a basic example, I do not understand the reason to put there self.tangerine="..." and why if I add self.order="order" everything works properly even if this parameter is not added into __init__(self, order)

class MyStuff(object):

    def __init__(self):
        self.tangerine="And now a thousand years between"

    def apple(self):
        print "I AM CLASSY APPLE!" 

thing=MyStuff()
thing.apple()

print thing.tangerine

So to drill down on this simple example, I added a variable in init:

class MyStuff(object):

    def __init__(self, order):
        self.tangerine="And now a thousand years between"
        self.order="order"

    def apple(self):
        print "I AM CLASSY APPLE!" 

thing=MyStuff()
thing.apple()

print thing.tangerine

Now I get an error:

Traceback (most recent call last):
  File "ex40_a.py", line 11, in <module>
    thing=MyStuff()
TypeError: __init__() takes exactly 2 arguments (1 given)

Thought it seems to me that there are 2 arguments there (tangerine(self) and order). Can anybody help me?

like image 685
Tommaso Lazzari Avatar asked Dec 16 '25 18:12

Tommaso Lazzari


1 Answers

Anatomy of your second code snippet:

# Define class named MyStuff which inherits from object
class MyStuff(object):

    # Define initializer method for class MyStuff
    # This method accepts 2 arguments: self and order
    # self will hold newly created instance of MyStuff
    def __init__(self, order):
        # Assign a string value to field tangerine of current instance
        self.tangerine="And now a thousand years between"
        # Assign a string value to field order of current instance
        self.order="order"
        # Note that second argument (order) was not used

    # Define apple method for class MyStuff
    # This method accepts 1 argument: self
    # self will hold the instance of MyStuff
    def apple(self):
        # Print a string to standard output
        print "I AM CLASSY APPLE!" 

# Create instance of MyStuff
# Initializer is called implicitly and self is set to new instance
# Second argument (order) is missing, so you get exception
thing=MyStuff()
# Correct invocation would be
thing = MyStuff("some string value")
# Call method apple of MyStuff instance - statement correct but won't be reached
# due to former exception
thing.apple()

# Print value of field tangerine of MyStuff instance to standard output - again
# statement correct but won't be reached due to former exception
print thing.tangerine

Things to read about:
- actual and formal function/method parameters
- string literals
- and of course Python classes

like image 152
ElmoVanKielmo Avatar answered Dec 19 '25 06:12

ElmoVanKielmo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!