Reputation: 93
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?
Upvotes: 3
Views: 8461
Reputation: 611
Looks ok but I assume you want the order value fed into your object. Also, generally you dont want to use print statements on your classes, instead return them and then print them else where in your code if you need
class MyStuff(object):
def __init__(self, order):
self.tangerine = "And now a thousand years between"
self.order = order
def apple(self):
return "I AM CLASSY APPLE!"
thing = MyStuff("I like strings and integer values")
print thing.order
print thing.tangerine
print thing.apple()
Output:
I like strings and integer values
And now a thousand years between
I AM CLASSY APPLE!
you specify the parameters you want to call your class with this:
def __init__(self, order):
self.order = order
if you dont want to call your class with anything and just use the string value do this:
def __init__(self):
self.order = "order"
Upvotes: 3
Reputation: 11290
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
Upvotes: 3