0

In order to create a code, I have decided to create a python class to just define some variables with default value. you can see this as "struct" in C.

the file is name : ScreenStructure.py

Inside I have defined this code

class ViewIdleScreen():
    def _init_(self):
        self.menu_access = "id/no_id/21"
        self.Call_app = "id/no_id/23"
        self.Email_app = "idno_id/24"
        self.Camera_app = "id/no_id/27"
        self.Browser_app = "id/no_id/26"
        self.Contacts_app = "id/no_id/9"
        self.Calendar_app = "id/no_id/10"
        self.Messaging_app = "id/no_id/11"
        self.Notes_app = "id/no_id/12"

    def Call_app(self):
        return self.Call_app

In the main file, I have added :

from ScreenStructure import ViewIdleScreen

later in the code of the main file:

IdleScreenView = ViewIdleScreen()
print IdleScreenView.Call_app()

but instead of displaying "id/no_id/23" it display

<bound method ViewIdleScreen.Call_app of <ScreenStructure.ViewIdleScreen instance at 0x02A16990>>
2
  • 1
    You've named the function and the attribute the same thing. You've also left out the double underscores around the init method, which means that's just a private method and not an initializer. Lastly, by convention, method names are lowercase in Python. Commented Apr 2, 2014 at 21:50
  • Looks like a good use for namedtuple(). Commented Apr 2, 2014 at 22:32

2 Answers 2

3

First, you're naming __init__ _init_. This is wrong. You need two underscores.

Second, you're setting an attribute Call_app there, but that's the same name as the method you define later:

def Call_app(self):
    return self.Call_app

In addition to being shadowed by the attribute (if __init__ were declared properly), this method returns the method itself, which is the bound method you're seeing.

Avoid the collision of attribute and method names, and name __init__ correctly

Sign up to request clarification or add additional context in comments.

1 Comment

ahh +1 better reading than me :P I thought it was strange that the method would shadow the member
0

you should not make functions named the same as data members

hello = "hello world"
def hello():
     print "goodbye!"

print hello

often times people will make a variable name preceded by an underscore or something

class X:
    def __init__(self,*args):
         self._x = "yellow"
    def x(self):
       return self._x

but as @mhlester points out a main problem is that you named __init__ incorrectly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.