-1

I have 3 python files .

MyModule.py

class MyClass:
    def __init__(self, name):
        self.name = name

    @classmethod
    def hello(self):
        print('Hello ' + self.name)

Loader.py

import pickle
from MyModule import *

me = pickle.load(file('my_pkl.pickle','rb'))
me.hello()

Dumper.py

import pickle
from MyModule import *

me = MyClass('Anil')
pickle.dump(me, open('my_pkl.pickle','wb'))

When I exceute Loader.py I get below error:

AttributeError: type object 'MyClass' has no attribute 'name'

How can I access name instance vaiable?

anilmwr
  • 477
  • 9
  • 16
  • Why is `me.hello()`? – thinkerou Apr 08 '15 at 11:40
  • @thinkerou I just want to call `hello()` method on `me` instance – anilmwr Apr 08 '15 at 11:42
  • But, the `me` and class `MyClass` does not matter. – thinkerou Apr 08 '15 at 11:49
  • 1
    Please read [here](http://stackoverflow.com/questions/12179271/python-classmethod-and-staticmethod-for-beginner) and [here](http://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python) and [here](http://stackoverflow.com/questions/5738470/whats-an-example-use-case-for-a-python-classmethod) about `@staticmethod` and `@classmethod`. – thinkerou Apr 08 '15 at 11:55

1 Answers1

2

You are using a instance method as a class method. Make it an instance method.

# remove @classmethod decorator
# called on instance: me.hello()

def hello(self):
    print('Hello ' + self.name) 
Malik Brahimi
  • 16,341
  • 7
  • 39
  • 70