传递变量,创建实例,自我,类的机制和用法:需要解释 [英] Passing variables, creating instances, self, The mechanics and usage of classes: need explanation
问题描述
我是新的到python。
我已经坐了这整天,我有点累了,请原谅我简短。我刚刚重写了一个工作程序,在一个类中的一堆函数,一切都乱了。我不知道是否是我,但我很惊讶,我找不到一个初学者的教程如何处理在网络上的类,所以我有几个问题。
第一,在类的 __ init __
部分中我已声明了一组变量 self.variable = something
这是正确的,我应该能够访问/修改这些变量在类的每个函数中使用 self.variable
在该函数中?换句话说,通过声明 self.variable
我已经做了这些变量,类的范围内的全局变量是否正确?
如果没有,如何处理自我?
其次,如何正确地传递参数到类?一些示例代码会很酷。
第三,如何调用类范围外的类的函数?一些示例代码会很酷。
Fouthly ,如何创建 class INITIALCLASS
在另一个类OTHERCLASS
中,将变量从 OTHERCLASS
传递到 INITIALCLASS
?一些示例代码会很酷。
我想从 OTHERCLASS
调用函数,参数为 INITIALCLASS
。到目前为止,我做的是
class OTHERCLASS():
def __init __(self,variable1,variable2 ,variable3):
self.variable1 = variable1
self.variable2 = variable2
self.variable3 = variable3
def someotherfunction(self):
something = somecode self.variable3)
self.variable2.append(something)
print self.variable2
def somemorefunctions(self):
self.variable2.append(variable1)
class INITIALCLASS():
def __init __(self):
self.variable1 = value1
self.variable2 = []
self.variable3 =''
self.DoIt = OTHERCLASS(variable1,variable2,variable3)
def somefunction(self):
variable3 = Somecode
#tried this
self.DoIt。 someotherfunctions()
#and this
DoIt.someotherfunctions()
不明白如何将变量传递给类或如何处理 self
,何时使用它,何时不使用它。我可能也不明白如何正确地创建一个类的实例。一般来说,我不明白课程的力学,所以请帮助我,并解释它,因为我不知道(我不,似乎)。
我在网上找到的都是超级简单的例子,没有帮助我。或者只是对类和类方法实例等的非常短的定义。
如果你想要,我可以给你原始代码,但是它很长。
class Foo(object):
#^ class name#^ inherited from object
bar =Bar#Class属性。
def __init __(self):
##^第一个变量是方法中的类实例。
##按照惯例称为self,但可以是任何您想要的名称。
#^双下划线(dunder)方法通常是特殊的。这一个
#在创建一个新实例后立即被调用。
self.variable =Foo#instance属性。
print self.variable,self.bar#< --- self.bar引用类属性
self.bar =Bar现在是Baz#< --- self.bar现在是实例属性
print self.variable,self.bar
def方法(self,arg1,arg2):
#这个方法有参数。你可以像这样调用:instance.method(1,2)
printin method(args):,arg1,arg2
printin method(attributes):,self.variable, self.bar
a = Foo()#这调用__init__(间接),输出:
#Foo bar
#Foo Bar现在是Baz
print a.variable#Foo
a.variable =bar
a.method(1,2)#output:
#in method(args):1 2
# (属性):bar Bar现在是Baz
Foo.method(a,1,2)#< ---与a.method(1,2)相同。这使得它更清楚一些参数self实际上是什么。
class Bar(object):
def __init __(self,arg):
self.arg = arg
self.Foo = Foo()
b = Bar(a)
b.arg.variable =something
print a.variable#something
print b.Foo.variable#Foo
I've been sitting over this the whole day and Im a little tired already so please excuse me being brief.
Im new to python.
I just rewrote a working program, into a bunch of functions in a class and everything messed up. I dont know if it's me but I'm very surprised I couldn't find a beginner's tutorial on how to handle classes on the web so I have a few questions.
First of all, in the __init__
section of the class I have declared a bunch of variables with self.variable=something
.
Is it correct that I should be able to access/modify these variables in every function of the class by using self.variable
in that function? In other words, by declaring self.variable
I have made these variables, global variables in the scope of the class right?
If not, how do I handle self?
Secondly, how do I correctly pass arguments to the class? Some example code would be cool.
Thirdly, how do I call a function of the class outside of the class scope? Some example code would be cool.
Fouthly, how do I create an Instance of the class INITIALCLASS
in another class OTHERCLASS
, passing variables from OTHERCLASS
to INITIALCLASS
? Some example code would be cool.
I Want to call a function from OTHERCLASS
with arguments from INITIALCLASS
. What I've done so far is.
class OTHERCLASS():
def __init__(self,variable1,variable2,variable3):
self.variable1=variable1
self.variable2=variable2
self.variable3=variable3
def someotherfunction(self):
something=somecode(using self.variable3)
self.variable2.append(something)
print self.variable2
def somemorefunctions(self):
self.variable2.append(variable1)
class INITIALCLASS():
def __init__(self):
self.variable1=value1
self.variable2=[]
self.variable3=''
self.DoIt=OTHERCLASS(variable1,variable2,variable3)
def somefunction(self):
variable3=Somecode
#tried this
self.DoIt.someotherfunctions()
#and this
DoIt.someotherfunctions()
I clearly didn't understand how to pass variables to classes or how to handle self
, when to use it and when not. I probably also didn't understand how to properly create an instance of a class. In general I didn't understand the mechanics of classes so please help me and explain it to me like I have no idea (which I don't, it seems). Or point me to a thorough video, or readable tutorial.
All I find on the web is super simple examples, that didn't help me much. Or just very short definitions of classes and class methods instances etc.
I can send you my original code if you guys want, but its quite long.
class Foo (object):
# ^class name #^ inherits from object
bar = "Bar" #Class attribute.
def __init__(self):
# #^ The first variable is the class instance in methods.
# # This is called "self" by convention, but could be any name you want.
#^ double underscore (dunder) methods are usually special. This one
# gets called immediately after a new instance is created.
self.variable = "Foo" #instance attribute.
print self.variable, self.bar #<---self.bar references class attribute
self.bar = " Bar is now Baz" #<---self.bar is now an instance attribute
print self.variable, self.bar
def method(self, arg1, arg2):
#This method has arguments. You would call it like this: instance.method(1, 2)
print "in method (args):", arg1, arg2
print "in method (attributes):", self.variable, self.bar
a = Foo() # this calls __init__ (indirectly), output:
# Foo bar
# Foo Bar is now Baz
print a.variable # Foo
a.variable = "bar"
a.method(1, 2) # output:
# in method (args): 1 2
# in method (attributes): bar Bar is now Baz
Foo.method(a, 1, 2) #<--- Same as a.method(1, 2). This makes it a little more explicit what the argument "self" actually is.
class Bar(object):
def __init__(self, arg):
self.arg = arg
self.Foo = Foo()
b = Bar(a)
b.arg.variable = "something"
print a.variable # something
print b.Foo.variable # Foo
这篇关于传递变量,创建实例,自我,类的机制和用法:需要解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!