-1

I want to access a method variable from within the class (not the same method)

def MyDef():
    class MyClass():
        def meth1(self):
            meth2(7)

        def meth2(self, user_value):
            value = 5 - 6*user_value

        if value > 2:
            # do something

MyDef()

I get the following error:

NameError: name 'value' is not defined.

Can someone please help me?

StardustGogeta
  • 3,331
  • 2
  • 18
  • 32
O'Jhene
  • 9
  • 4
  • 2
    `if self.value: ...` – Quentin Aug 01 '19 at 17:32
  • `value` is only available in `meth2`. You need to use the `self` argument of your method – Buckeye14Guy Aug 01 '19 at 17:33
  • 1
    Why not have `value` a property of the class (perhaps named `_value` if it isn't for external use) rather than a local variable in one of the methods? – John Coleman Aug 01 '19 at 17:33
  • Thanks for the quick response guys. I tried "if self.value..." and got this error - NameError: name 'self' is not defined – O'Jhene Aug 01 '19 at 17:35
  • I notice that you don't call `meth1` or `meth2` anywhere in your code. What value should `value` have when `if value > 2:` runs? Because as far as I can tell, nothing has been assigned to it yet. Keep in mind that code inside a class definition executes long before the user has a chance to create an instance of the class. – Kevin Aug 01 '19 at 17:37
  • You can't just refer to `self` in the class body. It should be used in a method. `self` is for instances of your class; not the class itself – Buckeye14Guy Aug 01 '19 at 17:39
  • Possible duplicate of [What is the purpose of the word 'self', in Python?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-the-word-self-in-python) – G. Anderson Aug 01 '19 at 17:41

2 Answers2

0

This is not an answer but you are missing a couple fundamental OOP things here. I can't have the whole thing in a comment so hopefully this helps.

  • You do not have any __init__ function
  • Classes are not functions (debatable). Just because you defined the class doesn't mean it will do anything. YOU have to create objects out of it.
  • Your meth1 is what I think you want as your __init__
  • Please read up on OOP in general and specifically in python. Perhaps this one can give you a headstart.
def MyDef():
    class MyClass():
        def __init__(self):
            self.meth2(0)
            self.do_something

        def meth2(self, user_value):
            self.value = 5 - 6*user_value

        @property
        def do_something(self):
            if self.value > 2:
                print "hey"

    myobject = MyClass()
    return myobject.value

Perhaps a better approach would be

def MyDef(value_entered):
    class MyClass():
        def __init__(self, user_value=value_entered):
            self.user_value = user_value
            self.meth2(self.user_value)
            self.do_something

        def meth2(self, user_value):
            self.value = 5 - 6*user_value

        @property
        def do_something(self):
            if self.value > 2:
                print "hey"

    myobject = MyClass()
    return myobject.value

MyDef(0)
Buckeye14Guy
  • 831
  • 6
  • 12
  • Thanks a ton!! This does what I'm looking to do. Your explanations are very useful. You are right, I'm lacking a lot of things on OOP because I'm very new and just beginning to get my hands dirty. – O'Jhene Aug 01 '19 at 17:55
0

You need to declare your variable inside of your class scope, but outside the method. Also your method needs to be static, and you need to call your method before attempting to use the modified variable... If your goal is to something specific to happen when you create an object of type MyDef, let me know, because this is not how you should use a class...

def MyDef():
class MyClass():
    def meth1():
        meth2(7)

    def meth2(user_value):
        return 5 - 6*user_value

    value = meth1()
    if value > 2:
        # do something

Again, this is the answer for your question, but please consider to edit your question to describe your goal with doing this, because this approach probably isn't the right way to do it...

abeagomez
  • 562
  • 1
  • 4
  • 16