Instantiaating your first object


#1

what is the difference
although output is the same

class Animal(object):
  def __init__(self,name):
        self.name = name 
        
    zebra = Animal("Jeffrey")
    print zebra.name
---------------------------------------------------------
class Animal(object):
    def __init__(self,name):
        self.name = name 
        
zebra = Animal("Jeffrey")
print zebra.name
    
    #used no indentation.

#2

the first code should give an error, you are still defining the class. You can't instantiate a class inside the class (given you are still defining a class).

instances should be created outside the class


#3

thank you very much, i understood now..
object oriented programming is little weird..
and its a little tough
any other good resources.?
i tried sololearn but not so great


#4

sololearn is good, its just fast paced

I learned from all over the place, i am sure you can find more resources using google


#5

solo learn is great but with OOP is not explained well ,
i am now also reading from" learning python the hard way.." which looks simplified...version


#6

i wouldn't go this far, i would stay with its fast paced

you can always check the official docs on classes:

https://docs.python.org/3/tutorial/classes.html

learn python the hard way is also good


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.