+ 1
new keywords and their confusing forms like dog("yellow",4).dog(self),dog.bark ,and dog.bark(1)
step by step,classâconstructor(or method or function?)âargumentsâattributesâ objects,then maybe instances ,too many new forms make me cofused
7 Antworten
+ 2
appreciate your explanations so much,although this OOP and "itself" philosophy is rather hard to me,ur explicit metaphors just like spring wind blowing into winter,I'll practice harder to get the whole thing,thanks!
+ 2
You could look up a description of OOP and probably find a better explanation. However, some languages call things different names, or have slightly different things they do.
the "itself" thing is pretty simple actually. imagine you have two doors, one red and one green. say you want to change door2 (the green one) from "closed" to "open". you would do so by using a method.
door2. swing()
this would make only door2 change states, but how?
def swing(self):
if self.state == "open":
self.state = "closed"
else:
self.state = "open"
the above method woulf be written in the Door class. the "self" tells the program that it is looking at whatever instance calls it (so door2 but not door1). it would be the same as writing this (outside the class, in the regular program)
def swing(arg):
if arg.state == "open":
arg.state = "closed"
else:
arg.state = "open"
swing(door2)
both the first code and this last will do the same thing, but the first one can be written inside the class as a method, where the second one is a function written in the program. it is better to do it as a method as every door that is made will have to be able to swing.
+ 2
Anyway,I'm getting familiar with the __init__ method gradually,which always involved in Math and Engineering problem solving
+ 1
Honestly, I found the sololearn Java definition to be one of the best I have seen, but I will give you a quick run down of it all.
everything you mentioned is part of an object, for Object Oriented Programming (OOP). The idea behind OOP is to make programming be a little more like the real world. In the real world, you have billions of different objects that do trillions of different things, and that all have different properties to them. These are called attributes. a car can be colored green or blue or white. a front door might be made of wood or metal. A dog could be a shepard or a labrador. these are all attributes.
A class is just the description of what an object is, but not of the object itself. such as a front door. you can probably imagine dozens of different front doors, and every one of them is a class of "front door". you could even have two identical doors, but they would still be separate objects.
in programming, objects have to be created (instantiated) and in order to do so they use something called a constructor. The idea of the constructor is to create an object with whatever attributes that specific object has. Like creating a door made of wood and painted green.
lastly (for this explanation), objects have methods. methods are functions that an object can perform. a door can open (this could change an attribute from "closed" to "open" and back), a car can drive, a dog can bark. these actions are called methods in OOP and are just functions specific to a class (remember a class is a description of the type of object, not the object itself).
+ 1
why we use __init__ in class
+ 1
we use __init__ to create the object because it is automatically called wjen a new object is instantiated. One of the biggest advantages to using __init__ is passing in kwargs (keyword args) to set as many attributes as we want/need when we create the object.
0
in order to access these things, you first have to create the object, then you can utilize the different things it can do/is made of.
class Dog() :
def__init__(self) : #the constructor
self.speak = "woof" #assigns an attribute to itself
def bark(self): #a method
print(self.speak) #accesses the attribute
fido = Dog() #this actually makes the object now
fido.bark() #calls the method from the object
"woof"
the top part is the class, which describes a dog. fido is the dog itself.