0

confusions in OOPs

  1. why 'var' is not taking the global value in function as it is taking in class

  2. can i create a local variable like 'a' in classes (say,c)?

  3. why cant define class's attribute inside class body , like the way i done in function?

  4. why 'f.a' is not callable but clss.c is callable? ( both assigned similarly )'''

(i am a beginner , my only option to ask and clear my concepts is to ask online python community for help bcz i m learning online).

#error statements commented out
var=10
def fun():
    #var=var*10
    a=10
    fun.b=20
fun()
print(var)
#print(a)
print(fun.b)
#print(fun.a)


class clss:
    var=var*var
    c=10
    #clss.d=20
#print(c)
#print(clss.d)
print(clss.var)
print(clss.c)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
harshit
  • 75
  • 4
  • 2
    There's a lot going on here. I suggest you break up your question into four different questions (each of which would have a different duplicate) – Mad Physicist Jun 28 '22 at 19:43
  • Does this answer your question? [Why does this UnboundLocalError occur (closure)?](https://stackoverflow.com/questions/9264763/why-does-this-unboundlocalerror-occur-closure) – Mad Physicist Jun 28 '22 at 19:46
  • "why 'var' is not taking the global value in function as it is taking in class" because class bodies are special cases, and this is one of those special things about the class body scope. – juanpa.arrivillaga Jun 28 '22 at 19:51
  • Duplicate of point #3: https://stackoverflow.com/q/34558324/2988730 – Mad Physicist Jun 28 '22 at 19:51
  • "can i create a local variable like 'a' in classes (say,c)?" you *are* doing that, in a sense, it's just that *the local variables of the class body namespace get turned into the class object namespace*. That is how class definitions work – juanpa.arrivillaga Jun 28 '22 at 19:51
  • "why cant define class's attribute inside class body , like the way i done in function?" because the class *doesn't exist yet (or the corresponding variable it is trying to refer to) when the class body is executed. On the other hand, a function already exists by the time it is called and the body executes – juanpa.arrivillaga Jun 28 '22 at 19:52
  • Also this: https://stackoverflow.com/q/46018872/2988730 – Mad Physicist Jun 28 '22 at 19:52
  • "why 'f.a' is not callable but clss.c is callable? ( both assigned similarly )''' Because , **class definitions are not function definitions**. the **whole point** of a class definitions is to create the *class namespace*. That namespace is *the accessible attributes of the class object*. Functions dont work like this at all. – juanpa.arrivillaga Jun 28 '22 at 19:53
  • @juanpa.arrivillaga. I'm unable to close as a dupe because OP originally had the tag python-3.8 instead of just python. If you were to close this question, I could update the list of dupes to address all of OP's concerns – Mad Physicist Jun 28 '22 at 19:53
  • @MadPhysicist oh, I just voted to close as too broad... I'm sure someone else will come along shorts – juanpa.arrivillaga Jun 28 '22 at 19:54
  • Also note, "callable" is the wrong terminology here. Attributes either exist (are defined) or not. *Neither* are callable in this case. Callable means you can *call it like a function*, i.e `mycallable(arg)` – juanpa.arrivillaga Jun 28 '22 at 19:59

0 Answers0