Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tushar Muhammad
Full Stack JavaScript Techdegree Student 508 PointsCan a variable own a method? I already know a string can. if so, can u provide an example? Thank you!
An example with a variable as an argument to the print function would be great! Thanks!
1 Answer
Alex Koumparos
Python Development Techdegree Student 36,888 PointsHi Tushar,
Any object can have methods.
Python is a true object-oriented language. As such, every data type, from built-in types like integers and strings, to custom types that you build yourself, are classes that can have methods.
We can see this ourselves in the Python interpreter:
>>> type(3)
<class 'int'>
Integer objects, such as the number 3, are instances of the int class.
>>> type("hello, world")
<class 'str'>
And string objects, in this case "hello, world", are instances of the str class.
So any concrete 'thing' in Python is going to be an object, and since any object can have methods, any thing can have methods.
Variables are not themselves objects, they are references to objects. Let's illustrate with a simple custom class, with a single simple method:
>>> class MyClass:
... def greeting(self):
... return "hello, world"
I'm not sure if the syntax above is all familiar to you. If not, just note that we've defined a custom class called MyClass. Instances of this class have a single method, called greeting which always returns a string, "hello, world".
Let's see what happens when we create an instance of this class:
>>> MyClass()
<__main__.MyClass object at 0x10b886128>
Python tells us that it has created a MyClass object and put it in memory location 0x10b886128. But we haven't assigned this to a variable, so it's just floating out there, untethered to anything, until the garbage collector comes along and destroys it.
Now let's create another object, but this time assign it to a variable, we'll call it my_obj:
>>> my_obj = MyClass()
Let's check that it's the same kind of thing as before:
>>> my_obj
<__main__.MyClass object at 0x10b886080>
Yep, same thing (but because it is a new instance of the class, it has a different memory location).
So my_obj is a reference to the MyClass object stored in memory location 0x10b886080. As a variable, my_obj doesn't have any methods of its own, it's just a reference to the MyClass object. But you can access the MyClass object's methods by addressing the variable. Remember we created the greeting method on MyClass? Let's access it:
>>> my_obj.greeting()
'hello, world'
Or, using print like you asked for:
>>> print(my_obj.greeting())
hello, world
But note that the methods don't belong to the variable, they belong to the object the variable points to. Let's prove this by changing what object my_obj points to:
>>> my_obj = 3
my_obj no longer points to a MyClass object, instead it now points to 3: an instance of the int class.
>>> my_obj.greeting()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'greeting'
Now we get an error message because objects of the int type don't have a greeting method.
Hope that clears everything up for you.
Cheers
Alex
Tushar Muhammad
Full Stack JavaScript Techdegree Student 508 PointsTushar Muhammad
Full Stack JavaScript Techdegree Student 508 PointsYo! this is amazing! Thanks, Alex! I like the whole demonstration of how objects are enveloped in these "memory slots."