


# will always be called with the instance first. # need to take self as the first argument. # all methods for a class, by default, are instance methods - and all of them # depending on what you are going for, there are a couple of different ways # level member called conn, or 2, access an instance member called conn # there is one of two things you are trying to do here - 1, access a class # should be written as nn = pymongo.Connection("localhost", 27017)Ĭonn = pymongo.Connection("localhost",27017) # will be cleaned up from memory, what you are thinking you're doing here # that is not being stored anywhere - when this method finishes, this variable # with that being said, you're initializing a local variable here called conn # conn is a class member, and would be accessed by doing nn # at the root module level - so in this example, only pymongo is a global # in this case, the conn variable is not a global. This method is called when you call the class, i.e. # allow you to initialize a particular instance of your class, represented # the _init_ method is the constructor method - this will

# generally, you don't instantiate an object pointer like this, # by doing class mongoDb(object):, otherwise you can just takeĬonn = object # here, you're defining a class member - global for all instances # you could inherit from object here, which is a good practice
#MONGODB PYTHON EXAMPLE CODE CODE#
So, firstly I'll explain what is going on with your code and explain why you're seeing what you're seeing: import pymongoĬlass mongoDB(): # you don't need ()'s here - only if you are inheriting classes It looks like you're mixing a couple of tutorials in different ways. So, syntax wise you have a number of problems.
