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 2 years ago.
Improve this question
What is the exact difference among these three and when do we use what?
class A:
pass
class A(object):
pass
or
class A():
pass
class A:
pass
It is implicitly subclass of object (as in other cases). I think it's most prefereable in case you don't inherit from anything (but it can depend on coding standards).
2)
class A(object):
pass
It is most explicit version.
3)
class A():
pass
In this case, as no class passed as parent class, by default it inherits from object.
So from functionality point of view, there is no difference. In Python3, all clasess inherit from object (even if it's not explixitly declared).
However, if you are using Python2, you need to pass superclass explicitly in every case.
Related
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 1 year ago.
Improve this question
When you create 2 classes in Python, does the second class always have to be a subclass or the child class? Is it possible to have two classes that have object as their parameters? Thanks!
class Bird(object):
def __init__(self, name):
self.name = name
print("A %s has feathers" % self.name)
class Seagull(object):
def __init__(self):
print("Seagulls can fly")
super().__init__('Seagull')
seagull = Seagull()
What is wrong with this code? It says that Seagull is an inheritance so its (object) should be Bird... but why?
Whether you have subclasses or independent classes depends on the logic you're implementing.
With a bird and a seagull, you'd probably want a subclass, because a seagull is a kind of bird:
class Bird(object):
...
class Seagull(Bird):
...
In other situations, you would want separate classes, not related to each other:
class Bird(object):
...
class Locomotive(object):
...
By the way, in Python 3 the (object) part is not needed when it's literally object, so we would normally write:
class Bird:
...
class Seagull(Bird):
...
class Locomotive:
...
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 1 year ago.
Improve this question
I have a class that contains some variables/states.
I would like to share those states with many other classes in my code.
I looked online and I saw that modules and singleton classes are a good way to this. I ended up creating a class and storing all the data as class attributes and accessing it via the class it self, like the example:
# foo.py
class Foo(object):
varx=45
def foo(x):
Foo.varx = x
And I would import the data as:
# bar.py
from foo import Foo
print(Foo.varx) #45
Foo.foo(5)
print(Foo.varx) #5
I would like to know if using classes attributes like this is an anti-pattern, or if there is a downside I am not seeing in this implementation.
Since your foo method is altering the state of the class Foo (rather than the state of any one instance of Foo), it would be more pythonic to use a classmethod in this case. Also, note that there is no need to explicitly inherit from object, as all python classes implicitly inherit from object.
class Foo:
varx = 45
#classmethod
def foo(cls, x):
cls.varx = x
Your current implementation of the foo method has the name of the class hardcoded into the implementation, which means that the implementation would break if you changed the name of the class. The implementation would also break if you had another class inheriting from Foo which you wanted to be able to implement the methods of Foo, as the class inheriting from Foo would have a different name.
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 4 years ago.
Improve this question
class A:
def __new__(cls):
return super(A,cls).__new__(cls)
No, it doesn't. It creates an instance of what you want.
Let's split down all your statements.
First, we declare a new class A:
class A:
By default, all classes inherit from object, so your declaration is the same as:
class A(object):
Next, you overwrite the definition for __new__:
def __new__(cls):
This is a function that takes the class object you want to instantiate.
Next, we call our base's class __new__ method:
super(A, cls) # This refers to the base class of A, which is object
.__new__(cls) # Calls object.__new__(cls), with cls being the class you want to instantiate
object.__new__ does the right job, in fact you can pass in any class you want and it will correctly instantiate it.
So, your A.__new__ is just a delegate to object.__new__, so really, your A.__new__ is completely useless, you can remove it and everything will work the same.
Having said that, when you do A(), this will trigger A.__new__(A), which will trigger object.__new__(A), so you'll end up with an instance of A.
If you were to have the following class:
class B(A):
def __new__(cls):
super(B, cls).__new__(cls)
The flow would be the same.
B() would trigger B.__new__(B) which would trigger A.__new__(B), which would trigger object.__new__(B), so you'd end up with an instance of B
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)
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 8 years ago.
Improve this question
I'm refactoring a program of mine. Basically I move all classes into a module.
Now I'm facing the problem that some of the module code is dependent on instances of a class I instantiated in my main program. Of course I could pass the instance to the method directly. Or pickle the instance. Or define the attribute as global. Which is the best way to go?
One possibility might be to pass the instance to the class upon instantiation:
class Bar(object):
def __init__(self, inst):
self.inst = inst
def method(self):
# use self.inst
inst = Foo()
bar = Bar(inst)