What is the best way to use a dictionary within a class. Do I have to assign the dict within the class ?

Thanks

There is no best way, there are different ways

>>> class A(object):
...  def __init__(self):
...   self.mydict = dict() # <-- a dict is added to every instance
... 
>>> a = A()
>>> a.mydict["foo"] = "bar"
>>> 
>>> class B(object):
...  shareddict = dict()  # <-- this dict can be used by all instances
...  def __init__(self):
...   pass
... 
>>> b = B()
>>> b.shareddict["foo"] = "bar"
>>> b2 = B()
>>> b2.shareddict
{'foo': 'bar'}
>>> 
>>> class C(object):
...  pass
... 
>>> c = C()
>>> c.__dict__  # <-- instances usually have a predefined dict to store attributes
{}

Thanks,

Currently I have ....

class Mac:
 def __init__(self,hexmac):
  self.hexmac = hexmac
 def resolve(self):
  return self.hexmac
  return mac_address
  print mac_address

mac_address = { \
'000000':'xerox0', \
'000001':'xerox1', \
'000002':'xerox2', \
}

Can i reference this dict within the class ?

Can i reference this dict within the class ?

Sure, in this case mac_address is a global variable, which can be accessed from any function's body in the same file, including class methods.

Ah ok. But if I do ...

import sys
from file import Mac

b = Mac('123')
b.resolve()

It doesnt return the dict ?

mac_address live alone in global space.
If you want it in your class you have pass it in as a instance variable or a agrument for for method resolve.

>>> from foo import Mac
>>> b = Mac(mac_address)
>>> b.resolve()
{'000000': 'xerox0', '000001': 'xerox1', '000002': 'xerox2'}

Or

class Mac:
    def __init__(self,hexmac):
        self.hexmac = hexmac

    def resolve(self, mac_address):
        return self.hexmac, mac_address

mac_address = { \
'000000':'xerox0', \
'000001':'xerox1', \
'000002':'xerox2', \
}

Use class.

>>> b = Mac('123')
>>> b.resolve(mac_address)
('123', {'000000': 'xerox0', '000001': 'xerox1', '000002': 'xerox2'})

Just an observation on your code ...

class Mac:
    def __init__(self,hexmac):
        self.hexmac = hexmac
    def resolve(self):
        return self.hexmac
        # after the above return the following will be ignored!!!
        return mac_address
        print(mac_address)

mac_address = {
'000000':'xerox0',
'000001':'xerox1',
'000002':'xerox2',
}

So rewrite your code as snippsat indicated already ...

class Mac:
    def __init__(self,hexmac):
        self.hexmac = hexmac
    def resolve(self):
        #print(mac_address)  # test
        return self.hexmac, mac_address


mac_address = {
'000000':'xerox0',
'000001':'xerox1',
'000002':'xerox2',
}

m = Mac('123')

# returns a tuple of 2 items
hx, ma = m.resolve()
print(hx)
print(ma)

I t would be better to give the dictionary to the class this way ...

class Mac:
    def __init__(self, hexmac, mac_address):
        self.hexmac = hexmac
        self.mac_address = mac_address
    def resolve(self):
        #print(self.mac_address)  # test
        return self.hexmac, self.mac_address


mac_address = {
'000000':'xerox0',
'000001':'xerox1',
'000002':'xerox2',
}

m = Mac('123', mac_address)

# returns a tuple of 2 items
hx, ma = m.resolve()
print(hx)
print(ma)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.