Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the main difference between class and def in python? Can a class in python interact with django UI (buttons)?
class is used to define a class (a template from which you can instantiate objects).
def is used to define a function or a method. A method is like a function that belongs to a class.
# function
def double(x):
return x * 2
# class
class MyClass(object):
# method
def myMethod(self):
print ("Hello, World")
myObject = MyClass()
myObject.myMethod() # will print "Hello, World"
print(double(5)) # will print 10
No idea about the Django part of your question sorry. Perhaps it should be a separate question?
class defines a class.
def defines a function.
class Foo:
def Bar(self):
pass
def Baz():
pass
f = Foo() # Making an instance of a class.
f.Bar() # Calling a method (function) of that class.
Baz() # calling a free function
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
When I write the __init__ method and assign attributes, can I access those attributes in other methods (functions) that I write in that class? If so, how is it done?
I've googled this but couldn't find an answer. I Haven't been able to wrap my head around this one.
Use self:
class MyClass:
def __init__(self):
self.name = 'John'
def other_method(self):
print(self.name)
other_method will print "John".
When you make a class and set an instance (like first_class = MyClass()) the def __init__(self): is run or initialised. Any variables in there, like self.name are able to be accessed from within the class and its functions, as well as when you use a class in another program. self kinda attaches that variable to that class.
Basically using Allure's example:
class MyClass:
def __init__(self):
self.name = "John"
def show_name(self):
print(self.name)
Then use MyClass's name outside of class, in a program:
firstClass = MyClass()#Initialise MyClass and its variables
print(firstClass.name)
Or:
firstClass= MyClass()
firstClass.show_name()
Both output:
'John'
(still putting up this answer for others, hope you don't mind :) )
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am new in OO programming. It is python 3.
Let be some example code:
class MyClass:
def __init__(self,a=1,b=2):
self.a=a
self.b=b
def function1(self):
c.self=salf.a/self.b + 5
return(c.self)
When I call a method from MyClass. i.e:
MyClass.function1()
it returns:
NameError: name 'self' is not defined.
I understand that if I initialize the code as:
class MyClass(object):
code etc...
It is all right if I put:
somevariable=MyClass; somevariable.function1()
It works... I do not know why this is happening.
Thank you so much !
You have to create an object of your class:
class MyClass:
def __init__(self,a=1,b=2):
self.a=a
self.b=b
def function1(self):
self.c=self.a/self.b + 5
return(self.c)
print(MyClass().function1())
MyClass() creates an object that can be used to access attributes in the class.
For a general instance:
m = MyClass()
print(m.function1())
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If I have some (and only one) class, A, in my file with some basic method,
class A(B):
def some_overrided_method()
return {'output': True}
what of the following would be better to use?
Static method inside class
class A(B):
def some_overrided_method(self)
return {'output': self.do_smth()}
#staticmethod
def do_smth(self):
return True
Function outside class
def do_smth():
return True
class A(B):
def some_overrided_method(self)
return {'output': do_smth()}
Of just some nested method inside
class A(B):
def some_overrided_method(self)
def do_smth():
return True
return {'output': do_smth()}
If it doesn't do anything with the class/instance, there is no point in it being a method of the class.
Just use a normal function.
A staticmethod is very rarely useful.
The only reason I can think of for using a staticmethod is if the function makes no sense to use outside of the class.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Is there a difference in the two ways of calling a static method in the same class shown below?
class MyClass:
def __init__(self):
self.do_something() #or MyClass.do_something()?
#staticmethod
def do_something():
pass
More specifically, particularly in the cases of refactoring (changing class name) and inheritance (as static methods can be inherited and overridden in Python)?
I can only think of one situation in which it would make a difference; what about when you inherit from MyClass? Compare:
>>> class MyClass(object):
def __init__(self):
self.do_something()
#staticmethod
def do_something():
print "Base class"
>>> class SubClass(MyClass):
#staticmethod
def do_something():
print "Sub class"
>>> s = SubClass()
Sub class
with:
>>> class MyClass(object):
def __init__(self):
MyClass.do_something()
#staticmethod
def do_something():
print "Base class"
>>> class SubClass(MyClass):
#staticmethod
def do_something():
print "Sub class"
>>> s = SubClass()
Base class
This is a contrived example, but you should consider what you think the appropriate behaviour should be.
Your example of refactoring to change the class name is a good one, too; self doesn't have to be updated whenever you rename the class, whereas if you have explicit MyClass references you have to update them all (a good IDE will do this for you, but if you can avoid it that's better).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a couple of functions defined in my code and I want to be able to call them in one of my class.
I want to pass one of their name as an argument so I can select the right function to call in my class.
I looked for such things on internet but what I found is how to call a function defined in a class inside the same or another class. I can't define my functions inside my class because they also call other functions
So there's not too much but there's my code :
class _fonction_use_:
def __init__(self,pos,function):
self.pos=pos
self.function=function
Where "function" would be the name of one of my functions defined outside the class.
So, if fonction_use belong to this class, I want something like fonction_use.function to return the function I would assigned it before.
Since functions are first class objects, you can pass them directly to your class.
def somefunc():
pass # do something
class MyClass(object):
def __init__(self, pos, function):
self.pos = pos
self.function = function
myclass = MyClass(0, somefunc)