codingdir logo sitemap sitemap |
Home
PHP
C#
C++
ANDROID
JAVA
JAVASCRIPT
PYTHON

Syntax for importing function from object in class (Python 2)


By : , Category : python

The answer to the question "Can I do this on the import line?" is no.

See the definition of the import statement in Python 2. The statement imports things from modules. There is a datetime class inside the datetime module. The best you can do is

from datetime import datetime

You already understand well what this does, as you used it perfectly in your question. It looks like you wanted to do

from datetime import datetime.strptime

but that is a syntax error because datetime.strptime is not an indentifier.

You can't say

from datetime.datetime import strptime

either because Python would look for a module named datetime.datetime.

The import statement just doesn't work the way you want it to.

Note that the author of the datetime module chose to make strptime a classmethod (using @classmethod) rather than a function. So if you want to use strptime without the class qualifier you will have to do what you did, namely assign to a variable named strptime.

ReLated :

If you look through the source of datetime.py, you'll notice this line (line 1290 in 3.4.2):

class datetime(date):

This is the class definition for the datetime class, which is inheriting from the previously-defined date class. Therefore, a datetime object (an instantiation of the datetime class) can access properties and methods defined in the date class, as long as they were not overridden by datetime.

You can read more about classes here, and about inheritance in particular here.

Having two underscores before a function means it is private. You can rename it to _func1 and it will work.

Alternatively, import __func1 as follows:

from package.coreFunc import __func1 as _func1

And use _func1 in __init__ instead of __func1.

Do you mean like this? Creating an instance within the class itself? Well this code works just fine in python 3.4, though I am unaware of future consequences this might cause.

class Animal(object):
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def description(self): 
        print (self.name)
        print (self.age)
    def testt(self):
        animo = Animal('johndoe', 5)
        animo.description()

horse = Animal('trevor', 4)
horse.testt()

May be it has to do with your definition of the fruit class. Below is the correct implementation of what you are trying to achieve here.

myFunction (fruit *apple)
{
...
}

int main()
{
   fruit *apple = new fruit;
   myFunction(apple);
}

In your code, apple is already a pointer to an object of type fruit. So you don't need to take its address again using the ampersand & operator. If your requirement was to pass a pointer to a pointer to an object (which I doubt is what you need), then your main() function was ok but myFunction would be defined as follows:

myFunction (fruit **apple)
{
...
}
Comments


Message :
Login to Add Your Comments .
How to disable registered OpenCL platforms on Windows?
Is Observable broken in Angular 2 Beta 3?
Cross-thread operation not valid when using Invoke
How to pass an IEnumerable or queryable list of properties from Controller to View
Finding numbers after a certain keyword using Python
Pocketsphinx recognizes random phrases in a silence
Passing non-thread-safe objects through thread-safe containers
React scroll nav
BizTalk WCF-BasicHttp Adapter does not allow Empty string for Service Certificate Props
Why property ''cause" of Exception is repeating forever?
Privacy Policy 2017 © codingdir.com All Rights Reserved .