I’ve been looking OOP and exercise 41.
I’m trying to wrap my head wrap around classes and object.
I found that website describs a class as
‘A class is a blueprint of an instance of an object’ - - which makes sense to me.
But then a few things confuses me in ‘Learn Code The Hard Way’
But might be me reading it wrong.
This is where I’m stuck:
object Two meanings: the most basic type of thing, and any instance of some thing.
I get the first part about the most basic type. fx ‘man’ is the basic object which is an instance of the class called ‘human’. ‘man’ has a bunch of properties such as name, hair and eye_color and some methods such as running, walking and sitting.
However I don’t get any ‘instance of some thing’?
instance What you get when you tell Python to create a class.
I might have gotten this wrong, but isn’t an object and instance of a class. ‘John’ and ‘Bob’ are two instances of the ‘human’ class?
Or is the human class that an instance?
Or is anything an instance fx. the ‘human’ class is one instance of the ‘mammal’ class. And ‘John’ and ‘Bob’ are then two instances of the the ‘human’ class?
self Inside the functions in a class, self is a variable for the instance/object being accessed.
self and init I really do not understand. To me it seems its variables that for what ever reason is used in classes. But I can’t figure out why they’re so important. Couldn’t they be called anything?
#I found the below which I understand the concept behind #but do not understand what self and __init__ does class Dog: def __init__(self, legs, colour): self.legs = legs self.colour = colour fido = Dog(4, "brown") spot = Dog(3, "mostly yellow")
class X(object): def init(self, J) class X has-a init that takes self and J parameters.
Is this the same as class X(Y): def init(self, J) or “make a class named X that is a Y that has a init that takes self and J parameters”?
or is (object) something special or can it be named anything?
Hope someone can shed some light on my confusion.