2

I have the following class:

classes/helper.py

import json

class Helper:
    def uJSONEncode(_, dict):
        print(type(_))
        return json.dumps(dict).decode('unicode-escape')

I instantiate the class as follows:

Python 2.7.9 (default, Feb 10 2015, 03:28:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin 
Type "help", "copyright", "credits" or "license" for more    information.
>>> from classes.helper import Helper
>>> h = Helper()
>>> h.uJSONEncode({"Asd": "asd"})
<type 'instance'>
\u'{"Asd": "asd"}'

Why does python pass (what I assume is) the instantiated object as the first parameter? How would I go around avoiding this behaviour?

Carey
  • 650
  • 1
  • 4
  • 16
  • 2
    If you don't need it to be a method then [don't make it a method](http://dirtsimple.org/2004/12/python-is-not-java.html). – Ignacio Vazquez-Abrams Sep 27 '15 at 06:28
  • Why did you create the class if it's not going to hold any state? – BrenBarn Sep 27 '15 at 06:29
  • I used a class because I didn't want to pollute the global scope. How do I namespace functions in this case then? – Carey Sep 27 '15 at 06:31
  • Putting a function in a module's namespace isn't polluting it, it's using it. See [this question](http://stackoverflow.com/questions/11788195/module-function-vs-staticmethod-vs-classmethod-vs-no-decorators-which-idiom-is). – BrenBarn Sep 27 '15 at 06:32
  • `from classes import helper` – Ignacio Vazquez-Abrams Sep 27 '15 at 06:34
  • I see what you mean @BrenBarn, thank you. I don't need a class in this, but instead can just import as a module. If you could reply to this as an answer, I'll accept it. – Carey Sep 27 '15 at 06:38

3 Answers3

1

You probably want to create a static method:

class Helper:
    @staticmethod
    def uJSONEncode(dict):
        print(type(_))
        return json.dumps(dict).decode('unicode-escape')

and then call it like this:

Helper.uJSONEncode({"Asd": "asd"})  
Cristian Lupascu
  • 39,078
  • 16
  • 100
  • 137
1

As others have mentioned you probably don't need a class here: just put that function as a stand-alone function into your module. Unlike in some other language (eg Java) Python doesn't force you to wrap things up into classes. Even if you have several related functions you probably don't need a class, unless those functions need to share state. Simply putting the related functions into one module is adequate encapsulation.


In Python, normal class methods receive the instance as the first argument. And it's normal to use self for that argument, so you'd write the method signature like

def uJSONEncode(self, dct):


In Python 2 you should derive your classes from object so that they're new-style classes, otherwise you get an old-style class, which has some limitations. Eg,

class Helper(object):

In Python 3, classes automatically inherit from object so you can use the syntax you used in your question, however it's still recommended to use the explicit object syntax.

One minor benefit of using a new-style class here is that the default representation of the instance's type (i.e., its class) is a little more informative:

import json

class Helper(object):
    def uJSONEncode(self, dct):
        print(type(self))
        return json.dumps(dct).decode('unicode-escape')

h = Helper()
print(h.uJSONEncode({"Asd": "asd"}))

output

<class '__main__.Helper'>
{"Asd": "asd"}


BTW, don't use dict as a variable name, as that shadows the built-in dict type, which can lead to mysterious & annoying bugs. The same goes for list, str, int, float, set, etc.


Lastly, _ has a special meaning in the interactive interpreter: it's the last result. Demo:

>>> 3+7
10
>>> _
10
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
1

You don't need a class just to write a function. Just do this:

def uJSONEncode(mydict):
    return json.dumps(mydict).decode('unicode-escape')

You can then import the module containing this function and use it as normal. Wrapping it in a class serves no purpose unless the class is going to actually do something (like store persistent state).

BrenBarn
  • 242,874
  • 37
  • 412
  • 384