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 trialmarkspenser
Courses Plus Student 256 PointsFloat/int is data type, but also a class ?
What is definition of class ? Type is data type. But class is class of what ? Can be float called as class too ? Class = type ?
Also Kenneth didn't explained what is method explicitly. It wasn't even in text under videos. I don't like this fast learning in here. I'm no programmer, I can't figure out some things, which are obvious to someone else
Yes/no
Why ?
4 Answers
Kenneth Love
Treehouse Guest TeacherI didn't explain what a method is because you're not creating them. You use a few that are attached to types/classes (because they're 99% the same thing). Basically, anything I don't explain explicitly isn't needed for this course.
Also, Paul Bentham, class variables are attributes. Properties are calculated values that we'll cover when I do an advanced OO course.
Paul Bentham
24,090 PointsThanks for clarifying Kenneth Love, looking forward to the advanced OO course.
markspenser
Courses Plus Student 256 Points1, So CLASS can never be called as TYPE, correct ?
- Str in meaning of Class, means that Str has some lower level structure elements under it's name. Str as class, is a general name for group of other functions, which are methods de facto.
2, But TYPE can be called as CLASS, correct ?
- Str is string type. it's type of value in python. it's an category of vaues.
- But Str, can be also Class. For example: str.rjust().
Thanks for reply Kenneth
Kenneth Love
Treehouse Guest TeacherThere's really no such thing as "type" in Python. Or, rather, "type" isn't something distinct. A dictionary is a variable of the dict
type and it's also an instance of the dict
class. If I make my own class, all instances of it could be said to be of type MyClass
or they'd, obviously, be an instance of MyClass
.
Everything is an object, even types :)
EDIT:
If you're talking about using the type()
function...don't use it. It's much better to use isinstance()
because that'll traverse the inheritance tree.
markspenser
Courses Plus Student 256 PointsKenneth, I'm very much confused.
1, We have values in python right ? Those are not homogenous. We may categorize them into: string, integer, float, nonetype, list, array ... ? So, it is types of values, isn't it ? Please explain.
2, About Object. Simply you say, Object can be called on anything in code. Is it like word 'something' or 'anything' in our human language ? So for example: fuction_a(Object) = Object Object(Object) = Object Object.Object(Object) = Object Object = Object Object == Object
Correct ?
Kenneth Love
Treehouse Guest Teacher- Values have a type, yes, but those types are, ultimately, classes. We don't have any sort of untouchable type concept in Python.
- Everything is an object and functions can accept and return objects, yes. I'm not 100% sure what you're asking here, actually.
markspenser
Courses Plus Student 256 Points"I'm not 100% sure what you're asking here, actually."
I will clarify. Imagine you are coding stuff in pc and someone is sitting near you. He checks monitor and start asking. What is that, what is this .... ?
On everything (every part of code) which he points out on monitor, can be called as Object ? Or when you still don't know, I'll ask: What is not an Object in code ?
Thank you
Kenneth Love
Treehouse Guest TeacherAh, OK, I think I understand better now.
Under the hood, yes, they're all objects. But just like every car, van, truck, semi, etc is an automobile, if you were walking through a car dealership, asking what this one or that one was, you wouldn't accept being told "it's an automobile" about every single one.
So, yes, they're all objects, but you'll have a much better time working with them and talking about them to other developers by calling them lists or dicts or strings or MyClass or whatever they actually are.
markspenser
Courses Plus Student 256 PointsIs Object just box or address in memory, storing a value ?
Kenneth Love
Treehouse Guest Teacher"Object" is a programming construct. Every variable (or instance, even if it doesn't have a name), just points to a spot in memory. That spot holds a string or a number or a pointer to some other variable or whatever that item is.
markspenser
Courses Plus Student 256 PointsNow you defined what's happening under the hood when I assign a value to a variable. That memory thing.
But still don't know definition of Object. Please try again. It's important for me.
Kenneth Love
Treehouse Guest Teachermarkspenser I don't know that you and I share enough vocabulary for me to go much farther. An object isn't anything in particular. It's an abstract programming concept of a thing that you can mold to match other things, either other concepts or real world items that you need in your program. Objects have attributes and methods. Every thing you deal with in Python is an object, so you don't really need to think about "is this an object?" because, yes, it is.
Really, at this point in Python Basics, and the Python track in general, having a 100% concrete grasp on what an object is or how it behaves won't help or hurt you. You'll come to understand them more as you move on though the content.
silashunter
Courses Plus Student 162 PointsKenneth, if I may quote you: "If you're talking about using the type() function...don't use it. It's much better to use isinstance() because that'll traverse the inheritance tree."
When I need to know the class/type of something (object ? hehe), I cannot put second argument into isinstance() function, beacause I don't know it.
But I can't also use type() function, because you told so :)
Now what ?
Kenneth Love
Treehouse Guest TeacherIt depends on usage. If I have a variable that I've never seen before and I'm having to do some sort of investigative work on it, sure, I'll use type()
to find out what kind of variable it is. In all my years of Python, this has never happened.
If, though, as is pretty common, I want to trigger something to happen based on the kind of variable something is, I'll use isinstance()
. This doesn't happen a lot because it's a bit more proscriptive than Python wants to be, but it does come up from time to time.
Paul Bentham
24,090 PointsPaul Bentham
24,090 PointsA Method is the same as a Function but inside a Class - the same as a Variable becomes a Property if it is inside a Class.
So say a Function like add(x, y) returns x+y, if that function was in a Class called Math, it would be a Method, which would be callable by Math.add(x+y).
Classes are a way of organising your code in a simpler to understand way.