Before publishing, I read more than a dozen messages searching from this site (python class static instance local variables)
https://stackoverflow.com/search?q=python+variables+class+static+instance+local
I added some results for future study, but none of them seemed to clarify to me if my thinking is right or wrong, so I feel that maybe something is missing about the basics (see below) ...
Are the terms “class variable” and “static variable” a reference to the same thing? After three Google searches, after reading through ~ 6 search articles that I could understand, I came to the conclusion that the class and the static variables are the same thing. But since I'm just learning the basics of Python and OOP, this conclusion may be wrong, so I want to find flaws in my reasoning before I continue to learn with the wrong mindset. In the code below:
class Variables():
scVar = 3
def __init__(self, a, b):
self.iVar1 = a
self.iVar2 = b
def getLocalVar3(self):
localVar1 = 17
localVar2 = 100
localVar3 = localVar1 + localVar2
return localVar3
Is "scVar" both a class variable and a static variable ("class" and "static" variables are synonyms)?
, , . , scVar - ; iVar1 iVar2 - ; localVar1, localVar2 localVar3 . , -, ?
, cirosantilli, , , - , . . Python vew, . , , , , , ? , Python. , , , ; , [enter], , , . , Python. , , OO Python - , , , .
" : ". - , , " ..." , Python - ? , . "... ". , , , classobj , Python , ? , . , *.py , IDLE, Python 2.7.x( ):
>>> Variables.scVar
3
>>> Variables.iVar1
AttributeError: class Variables has no attribute 'iVar1'
>>> instance = Variables(5, 15)
>>> instance
<__main__.Variables instance at 0x02A0F4E0>
>>> Variables
<class __main__.Variables at 0x02A0D650>
>>> instance.scVar
3
>>> instance.iVar1
5
>>> instance2 = Variables(25, 35)
>>> instance2.scVar
3
>>> Variables.scVar = Variables.scVar * 100
>>> Variables.scVar
300
>>> instance.scVar
300
>>> instance2.scVar
300
>>> instance.scVar = 9999
>>> Variables.scVar
300
>>> instance.scVar
9999
>>> instance2.scVar
300
>>> type(Variables)
<type 'classobj'>
>>> type(instance)
<type 'instance'>
" : ". - , , , ? , , ; . , , , - 'click'. , , " ", ... ( - 12 ) ? , , , , , . , , , ( ) - "Python ; , . , " ", ?
" , :" . , , OO Python, , ( ).
class Foo():
integer = 10
float = 6.37
string = 'hello'
boolean = True
idkyet = None
def __init__(self):
self.a = 'iv_a'
self.b = 'iv_b'
self.c = 'iv_c'
def Func(self):
self.g = 'g'
h = 'h'
i = 'i'
return 'g' + 'h' + 'i'
>>> Foo
<class __main__.Foo at 0x02A1D650>
>>> type(Foo.integer)
<type 'int'>
>>> type(Foo.float)
<type 'float'>
>>> type(Foo.string)
<type 'str'>
>>> type(Foo.boolean)
<type 'bool'>
>>> type(Foo.idkyet)
<type 'NoneType'>
>>> type(Foo)
<type 'classobj'>
>>> import os
>>> type(os.getcwd() + '\\Test.py')
<type 'str'>
>>> type(os)
<type 'module'>
>>> f = Foo()
>>> type(f)
<type 'instance'>
>>> type(f.Func)
<type 'instancemethod'>
>>> type(f.Func())
<type 'str'>
>>> f.Func
<bound method Foo.Func of <__main__.Foo instance at 0x02A25AF8>>
>>> Foo.Func
<unbound method Foo.Func>
>>> type(f.a)
<type 'str'>
>>> type(Foo.a)
AttributeError: class Foo has no attribute 'a'
>>> type(Foo.self.a)
AttributeError: class Foo has no attribute 'self'
, " Foo():" , , , . , "cafepy...", ( ). . , , , . , . ( "***" ), , .... ... ???... < , , , , . , - , , .
, , , "MRO", ... , , .